为什么我无法在Windows手机编程中使用FindResource()?

时间:2013-11-28 07:07:25

标签: c# xaml windows-phone-8

我想在C#Windows手机编程中使用FindResource()来改变控件的样式,但我无法做到。

  

play_btn.Style = FindResource("btnplay") as Style;

这会产生错误:当前上下文中不存在。

1 个答案:

答案 0 :(得分:6)

如果您的样式是在App.xaml的Resources中定义的,则必须使用:

play_btn.Style = App.Current.Resources["btnplay"] as Style;

否则(例如MainPage.xaml,SecondPage.xaml ...):

play_btn.Style = this.Resources["btnplay"] as Style;

或者您可以将TryFindResource实现为扩展方法:“How to implement the missing TryFindResource”。