c# - 为什么我不能在 Windows 手机编程中使用 FindResource()?

标签 c# xaml windows-phone-8

我想在 C# Windows Phone 编程中使用 FindResource() 来更改控件的样式,但我做不到。

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

这给出了一个错误:在当前上下文中不存在。

最佳答案

如果您的样式在 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 ”。

关于c# - 为什么我不能在 Windows 手机编程中使用 FindResource()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20259770/

相关文章:

c# - 毛玻璃窗上的非客户绘画

c# - 对象实例内部运行的 main 与纯静态有什么区别?

c# - 在 OpenQA.Selenium (C#) 中找不到 WebDriverWait 类

c# - 转换器无法将类型 'system.datetime' 的值转换为 Windows Phone 8.1 Datepicker 中的类型 'datetime'

windows-phone-7 - 在 Windows Phone 7.1/可移植类库中使用 System.Lazy

c# - Windows Phone 应用程序中用户控件的绑定(bind)属性

c# - 日期转换问题

c# - 如何创建自定义 WPF 样式库

WPF 数据网格 : How do you set the style of the first row only?

c++ - Windows Phone 中没有 FileIO?