Why can't I use FindResource () in Windows Phone Programming?

I want to use FindResource()in C # Windows to program my phone to change the style of the control, but I cannot.

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

This gives an error: does not exist in the current context.

+4
source share
1 answer

If your style is defined in Resourcesfor App.xaml, you should use:

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

otherwise (e.g. MainPage.xaml, SecondPage.xaml ...):

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

Or you can implement TryFindResourceas an extension method: How to implement the missing TryFindResource file .

+6
source

All Articles