【问题标题】:Why am I not able to use FindResource() in Windows phone programming?为什么我不能在 Windows 手机编程中使用 FindResource()?
【发布时间】:2013-11-28 07:07:25
【问题描述】:

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

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

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

【问题讨论】:

  • btnplay 资源在哪里定义?它是否包含在正确的位置(如 app.xaml / window.xaml)?
  • btnply 是一种按钮样式。我喜欢在运行时更改此按钮的图标。

标签: c# xaml windows-phone-8


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多