title: Update resource in Universal Windows App XAML
body: <p>I need to change a color of application's TextBlocks at runtime in an Universal Windows App. </p>
  
  <p>Universal Windows Apps don't support Dynamic Resources and I've been unsuccessfully exploring a few different ways to change color of TextBlock</p>
  
  <pre><code>&lt;TextBlock Text="Test" Style="{StaticResource MyText}"/&gt;
  </code></pre>
  
  <p>using the style</p>
  
  <pre><code>&lt;Style x:Key="MyText" TargetType="TextBlock"&gt;
      &lt;Setter Property="Foreground" Value="{StaticResource TextColor}" /&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>My question is: <strong>How can I change the color of the TextBlock at runtime?</strong></p>
  
  <p>What follows are all attempts to change the color:</p>
  
  <hr>
  
  <p>Initially, I followed this article+video <a href="http://blog.jerrynixon.com/2013/01/walkthrough-dynamically-skinning-your.html" rel="nofollow">Dynamically Skinning Your Windows 8 App</a> and I stored <code>TextColor</code> in a separate dictionary file that I can swap in and out of <code>MergedDictionaries</code></p>
  
  <ul>
  <li><code>Day.xaml</code> contains <code>&lt;SolidColorBrush x:Key="TextColor" Color="#FFDDEEFF" /&gt;</code></li>
  <li><code>Night.xaml</code> contains <code>&lt;SolidColorBrush x:Key="TextColor" Color="#FFFFDD99" /&gt;</code></li>
  </ul>
  
  <p>In code:</p>
  
  <pre><code>    ResourceDictionary _nightTheme = new ResourceDictionary() { Source = new Uri("ms-appx:///Themes/Night.xaml") };
      ResourceDictionary _baseTheme = new ResourceDictionary() { Source = new Uri("ms-appx:///Themes/MyApp.xaml") };
  
  // OnLaunched - I set a default theme to prevent exceptions
      Application.Current.Resources.MergedDictionaries.Add(_dayTheme);
  
  // Method that changes theme:
          if (NightFall)
          {
              Application.Current.Resources.MergedDictionaries.Remove(_dayTheme);
              Application.Current.Resources.MergedDictionaries.Add(_nightTheme);
          }
          else
          {
              Application.Current.Resources.MergedDictionaries.Remove(_nightTheme);
              Application.Current.Resources.MergedDictionaries.Add(_dayTheme);
          }
  </code></pre>
  
  <hr>
  
  <p>When this didn't work, I thought I need to clear the dictionaries:</p>
  
  <pre><code>    ResourceDictionary _baseTheme = new ResourceDictionary() { Source = new Uri("ms-appx:///Themes/MyApp.xaml") };
  // Method that changes theme:
          Application.Current.Resources.MergedDictionaries.Clear();
          Application.Current.Resources.MergedDictionaries.Add(_baseTheme);
          if (NightFall)
          {
              Application.Current.Resources.MergedDictionaries.Add(_nightTheme);
          }
          else
          {
              Application.Current.Resources.MergedDictionaries.Add(_dayTheme);
          }
  </code></pre>
  
  <hr>
  
  <p>I also tried to refresh the frame in the method that changes dictionaries, to no avail</p>
  
  <pre><code>        var frame = Window.Current.Content as Frame;
          frame.Navigate(frame.Content.GetType());
  </code></pre>
  
  <hr>
  
  <p>In another attempt I tried to create a dictionary at runtime and update it</p>
  
  <pre><code>ResourceDictionary _dynamicTheme = new ResourceDictionary();
  // OnLaunched
          _dynamicTheme.Add("TextColor", new SolidColorBrush(Windows.UI.Colors.Chocolate));
          Application.Current.Resources.MergedDictionaries.Add(_dynamicTheme);
  // Method that changes theme
          _dynamicTheme.Remove("TextColor");
          _dynamicTheme.Add("TextColor", new SolidColorBrush(NightFall ? Windows.UI.Colors.Chocolate : Windows.UI.Colors.Cornsilk));
  </code></pre>
  
  <hr>
  
  <p>Finally, I realized that perhaps <code>StaticResource</code> makes the color immutable, so I decided to give <code>ThemeResource</code> a try. I've modified my themes:</p>
  
  <pre><code>&lt;Style x:Key="MyText" TargetType="TextBlock"&gt;
      &lt;Setter Property="Foreground" Value="{ThemeResource MyTextColor}" /&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p><code>Day.xaml</code></p>
  
  <pre><code>&lt;ResourceDictionary.ThemeDictionaries&gt;
      &lt;ResourceDictionary x:Key="Default"&gt;
          &lt;SolidColorBrush x:Key="MyTextColor" Color="#FFDDEEFF" /&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/ResourceDictionary.ThemeDictionaries&gt;
  </code></pre>
  
  <p><code>Night.xaml</code></p>
  
  <pre><code>&lt;ResourceDictionary.ThemeDictionaries&gt;
      &lt;ResourceDictionary x:Key="Default"&gt;
          &lt;SolidColorBrush x:Key="MyTextColor" Color="#FFFFDD99" /&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/ResourceDictionary.ThemeDictionaries&gt;
  </code></pre>
  
  <p>I swapped the methods in and out of the <code>Application.Current.Resources.MergedDictionaries</code> just like in previous attempts.
  Again, the color doesn't change, even if I <em>fake-refresh</em> the <code>Frame</code></p>