簡體   English   中英

無法從應用程序級別獲取資源

[英]Can't get resource from application level

我的app.xaml:

<Application.Resources>
    <RadialGradientBrush x:Key="myBrush">
        <GradientStop Color="#FFC44EC4" Offset="0" />
        <GradientStop Color="#FF829CEB" Offset="1" />
        <GradientStop Color="#FF793879" Offset="0.669" />
    </RadialGradientBrush>
</Application.Resources>

我在這里嘗試使用它:

private void btnOK_Click(object sender, RoutedEventArgs e)
    {
        RadialGradientBrush b = (RadialGradientBrush)Resources["myBrush"];
        //b.GradientStops[1] = new GradientStop(Colors.Red,0.0);
    }

但我不能使用“b”因為它在定義后為空。 我怎樣才能獲得該資源?

你是從控件的資源'繪圖',嘗試其中一個...

res = this.FindResource("myBrush"); // this 'walks' the tree and will find it
res = Application.Current.Resources["myBrush"]; // or reference app resources directly
res = App.Current.TryFindResource("myBrush");

希望這可以幫助

當您嘗試從btnOK_Click事件到達資源時,我將假設該方法屬於窗口對象。 所以,您正在尋找錯誤位置的資源。 您必須改為引用應用程序的資源字典。

所以,我的建議是:

private void btnOK_Click(object sender, RoutedEventArgs e) 
{ 
    RadialGradientBrush b = (RadialGradientBrush)Application.Current.Resources["myBrush"]; 
    b.GradientStops[1] = new GradientStop(Colors.Red,0.0); 
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM