[英]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.