Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows Phone: how to change the Application style programmatically
    primarykey
    data
    text
    <p>In my Windows Phone 8 app, I have some implicit styles defined in a xaml file at the location <strong>/Styles/DefaultStyles.xaml</strong></p> <p>I have a similar file but with different colors, fonts, etc ... defined at <strong>/Styles/GreenStyles.xaml</strong>.</p> <p>I reference the default style file in my App.xaml as follows :</p> <pre><code>&lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="Styles/DefaultStyles.xaml"/&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; </code></pre> <p>I want to make my app switch its implicit styles from the other styles file (GreenStyles) programmatically.</p> <p>How can I achieve this ?</p> <p>**</p> <blockquote> <p><strong>UPDATE:</strong></p> </blockquote> <p>I manged to change the source of the resource dictionary as follows:</p> <pre><code>ResourceDictionary style = App.Current.Resources.MergedDictionaries.ToList()[0]; string source = String.Format("/ApplicationName;component/Styles/GreenStyles.xaml"); style.Source = new Uri(source, UriKind.Relative); </code></pre> <p><strong>Note:</strong> the word component must be written like that to avoid exceptions</p> <p>Now I have an issue: only the <strong>Implicit styles</strong> (the ones that do not have a <strong>x:Key</strong> attribute) are switched when the source of the dictionary changes.</p> <p>any other style with a specified key and defined twice (with different attributes) in both files, will not be reflected in the UI.</p> <p>so if I have these files: <strong>DefaultStyles.xaml:</strong> </p> <pre><code> &lt;Style x:Key="MainGrid" TargetType="Grid"&gt; &lt;Setter Property="Background" Value="Red"/&gt; &lt;/Style&gt; &lt;Style TargetType="TextBlock"&gt; &lt;Setter Property="Foreground" Value="Red"/&gt; &lt;Setter Property="FontSize" Value="24"/&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p><strong>And: GreenStyles.xaml:</strong></p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"&gt; &lt;Style x:Key="MainGrid" TargetType="Grid"&gt; &lt;Setter Property="Background" Value="Green"/&gt; &lt;/Style&gt; &lt;Style TargetType="TextBlock"&gt; &lt;Setter Property="Foreground" Value="Green"/&gt; &lt;Setter Property="FontSize" Value="24"/&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>and I switched the source to point to <strong>GreenStyles.xaml</strong>, any Grid with the style <strong>MainGrid</strong> will still have it's background to <strong>Red</strong>.</p> <p>What can be the reason for this ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload