Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 239
mohammad madani Avatar asked Mar 22 '23 11:03

mohammad madani


1 Answers

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".

like image 188
Jakub Krampl Avatar answered Apr 07 '23 15:04

Jakub Krampl