views:

229

answers:

1

Hello, how to go from:

<Application
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="teste.App">
 <Application.Resources>
  <!-- Resources scoped at the Application level should be defined here. -->
  <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
    **<ResourceDictionary Source="Green.xaml"/>**  
   </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
 </Application.Resources>
</Application>

To:

<Application
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="teste.App">
 <Application.Resources>
  <!-- Resources scoped at the Application level should be defined here. -->
  <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
    **<ResourceDictionary Source="Blue.xaml"/>**  
   </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
 </Application.Resources>
</Application>

Dynamically, when users choose the theme... Is it possible? Thank you Josi

A: 

You could change the resource dictionary dynamically in the code behind, on the load of the page.xaml:

Application:

//Load in resources either from application 
        ResourceDictionary dictionary = Application.Current.Resources as ResourceDictionary;

Then once you have the resource dictionary you can apply it using simple logic:

//Load in xaml
        if (user1)
            themefile = @"[Assembly];component/Themes/blue.xaml";
        else
            themefile = @"[Assembly];component/Themes/green.xaml";

NB: [Assembly] is the namespace of the component where the xaml file exists.

Then merge it into the dictioanry for the page with:

 XDocument xaml = XDocument.Load(themefile);
 ResourceDictionary rd = XamlReader.Load(xaml.ToString()) as ResourceDictionary;

        dictionary.MergedDictionaries.Add(rd);
Aim Kai
Is there a way to apply it to the whole application setting the new theme once?
Josimari Martarelli
What namespace is XDocument in?
Josimari Martarelli
XDocument is in the System.Linq namespace.
Aim Kai
In regards to "Is there a way to apply it to the whole application setting the new theme once?" - you could put the code in the constructor method.
Aim Kai