locked
keyless styles not being pick up. RRS feed

  • Question

  • Hi

    I have multiple resource files that make up a skin. What i would like to do is allow developers to add the skin by adding just one resource file.

    So i created a MainSkin.xaml file that looks like this:

     

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
     <ResourceDictionary.MergedDictionaries>
     <ResourceDictionary Source="ResourceDictionaries/Brushes.xaml"></ResourceDictionary>
     <ResourceDictionary Source="ResourceDictionaries/ControlStyles.xaml"></ResourceDictionary>
     </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

     

    in the application i can refernce this skin by doing this:

     

    <Application x:Class="TestResource.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      StartupUri="MainWindow.xaml">
     <Application.Resources>
     <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/SkinApp;component/Skins/MainSkin.xaml "></ResourceDictionary>  
      </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
     </Application.Resources>
    </Application>
    
    

     

    Unfortunately the keyless style are not being applied to the application unless i add a reference to the controlstyle.xaml resource dirctionary in the application like so:

     

    <Application x:Class="TestResource.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      StartupUri="MainWindow.xaml">
     <Application.Resources>
     <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/SkinApp;component/Skins/DefaultSkin.xaml"></ResourceDictionary>
      <ResourceDictionary Source="/SkinApp;component/Skins/ResourceDictionaries/ControlStyles.xaml"></ResourceDictionary>
      </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
     </Application.Resources>
    </Application>
    
    

     

    I am able to reference styles with keys as usual.

    The really annoying part is the visual designer in Visual studio pickes up the keyless styles but during runtime the application looks unskinned.

    Is there a way i can wrap all my skin resources into one reference and apply keyless styles?

    Cheers,

     

    the haggis



    Tuesday, April 5, 2011 3:05 PM

Answers

  • The problem is with the tree structure you are using. Due to an obscure optimization, if a ResourceDictionary does not contain a default style it won't look deeper than one level from there. Unfortunately, the designer does, which causes the inconsistent behaviour you are witnessing.

    Once you find the cause, the solution is trivial, even though not exactly elegant: you just need to place a default style somewhere in your tree so that the optimization isn't triggered. For instance, you could use something like this in your App.xaml:

    <Application x:Class="TestResource.App"
    
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
     StartupUri="MainWindow.xaml">
    
     <Application.Resources>
    
     <ResourceDictionary>
    
     <ResourceDictionary.MergedDictionaries>
    
     <ResourceDictionary Source="/SkinApp;component/Skins/DefaultSkin.xaml"></ResourceDictionary>
    
     <ResourceDictionary Source="/SkinApp;component/Skins/ResourceDictionaries/ControlStyles.xaml"></ResourceDictionary>
    
     </ResourceDictionary.MergedDictionaries>
    
    
    
     <Style TargetType="Viewport3D" />
    
    
    
     </ResourceDictionary>
    
     </Application.Resources>
    
    </Application>
    
    
    
    

    Of course this is not going to work correctly if you are using Viewport3D in your app; just choose some control you don't need or create a dummy one exactly for this purpose.

    And keep in mind that you are defeating an optimization that was probably put in place for a good reason...

    HTH
    --mc
     

    • Proposed as answer by Min Zhu Wednesday, April 6, 2011 3:05 AM
    • Marked as answer by the haggis Thursday, April 7, 2011 3:46 PM
    Tuesday, April 5, 2011 5:20 PM

All replies

  • The problem is with the tree structure you are using. Due to an obscure optimization, if a ResourceDictionary does not contain a default style it won't look deeper than one level from there. Unfortunately, the designer does, which causes the inconsistent behaviour you are witnessing.

    Once you find the cause, the solution is trivial, even though not exactly elegant: you just need to place a default style somewhere in your tree so that the optimization isn't triggered. For instance, you could use something like this in your App.xaml:

    <Application x:Class="TestResource.App"
    
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
     StartupUri="MainWindow.xaml">
    
     <Application.Resources>
    
     <ResourceDictionary>
    
     <ResourceDictionary.MergedDictionaries>
    
     <ResourceDictionary Source="/SkinApp;component/Skins/DefaultSkin.xaml"></ResourceDictionary>
    
     <ResourceDictionary Source="/SkinApp;component/Skins/ResourceDictionaries/ControlStyles.xaml"></ResourceDictionary>
    
     </ResourceDictionary.MergedDictionaries>
    
    
    
     <Style TargetType="Viewport3D" />
    
    
    
     </ResourceDictionary>
    
     </Application.Resources>
    
    </Application>
    
    
    
    

    Of course this is not going to work correctly if you are using Viewport3D in your app; just choose some control you don't need or create a dummy one exactly for this purpose.

    And keep in mind that you are defeating an optimization that was probably put in place for a good reason...

    HTH
    --mc
     

    • Proposed as answer by Min Zhu Wednesday, April 6, 2011 3:05 AM
    • Marked as answer by the haggis Thursday, April 7, 2011 3:46 PM
    Tuesday, April 5, 2011 5:20 PM
  • Hi the haggis,

    You can also use the following implicit style.

    <Style TargetType="Viewport3D" BaseOn="{StaticResource {x:Type Viewport3D}}"/>
    

    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 6, 2011 3:47 AM