[英]Can't get resource from application level
My app.xaml: 我的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>
Here I'm trying to use it: 我在这里尝试使用它:
private void btnOK_Click(object sender, RoutedEventArgs e)
{
RadialGradientBrush b = (RadialGradientBrush)Resources["myBrush"];
//b.GradientStops[1] = new GradientStop(Colors.Red,0.0);
}
But I can't use "b" cause it is null after defining. 但我不能使用“b”因为它在定义后为空。 How can I get that resource? 我怎样才能获得该资源?
You're 'drawing' from the control's resources, try one of these instead... 你是从控件的资源'绘图',尝试其中一个...
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");
hope this helps 希望这可以帮助
As you are trying to reach the resource from an btnOK_Click event, I will assume that method belongs to a window object. 当您尝试从btnOK_Click事件到达资源时,我将假设该方法属于窗口对象。 So, you are looking for the resource in the wrong location. 所以,您正在寻找错误位置的资源。 You have to reference your application's resource dictionary instead. 您必须改为引用应用程序的资源字典。
So, my suggestion is: 所以,我的建议是:
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.