mohammad madani
mohammad madani

Reputation: 135

Why am I not able to use FindResource() in Windows phone programming?

I want to use FindResource() in C# Windows phone programming to change the style of a control, but I am not able to.

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

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

Upvotes: 4

Views: 5105

Answers (1)

Jakub Krampl
Jakub Krampl

Reputation: 1794

If your style is defined in Resources of the App.xaml, you have to 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 TryFindResource as extension method: "How to implement the missing TryFindResource".

Upvotes: 7

Related Questions