9

If you put style in app.xmal, then you can get the style with the code like:

Style = Application.Current.Resources["myStyle"] as Style;

But if I put style in a dictionary resource of a separate file Styles.xmal like:

 <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>                
                <ResourceDictionary Source="Assets/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

Then how to get the style in code?

KentZhou
  • 24,805
  • 41
  • 134
  • 200

2 Answers2

8

Found out reason: code

Style = Application.Current.Resources["myStyle"] as Style;

works fine. My problem is caused by some other factors.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
KentZhou
  • 24,805
  • 41
  • 134
  • 200
0

tip:

  • be careful not to do this.Resources for the current object by mistake if what you're looking for is in Styles.xaml or Resources.xaml
  • also don't expect to see all the resources in the 'add watch' dialog for Application.Current.Resources. ones defined in XAML won't be there (at least not on the top level)
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689