2013-11-28 48 views
4

我想在C#Windows手機編程中使用FindResource()來改變控件的樣式,但我無法。爲什麼我無法在Windows phone編程中使用FindResource()?

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

這給出了一個錯誤:在當前上下文中不存在。

+0

'btnplay'資源定義在哪裏?它是否包含在正確的位置(如app.xaml/window.xaml)? – sthotakura

+0

'btnply'是一種button.i的樣式,就像運行時更改此按鈕的圖標一樣。 –

回答

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」。

相關問題