Programmatically getting app XAML resource from MergedDictionaries
<p>Universal Windows 8.1 Store Project here.</p>

<p>I need to programmatically get a resource from merged dictionaries of an application given a resource name. </p>

<p>I came up with a utility method which gets me what I want, but looks rather ugly to me:</p>

<pre><code>public static async Task&lt;T&gt; GetAppResource&lt;T&gt;(string key) where T:class
{
  T resource = default(T);
  await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
    CoreDispatcherPriority.Normal,
    ()=&gt;{
      foreach (var d in Application.Current.Resources.MergedDictionaries) {
        foreach (var pair in d) {
          if (pair.Key.ToString() == key &amp;&amp; pair.Value is T) {
            resource = pair.Value as T;
            goto End;
          }
        }
      }
      End:;
    }
  );
  return resource;
}
</code></pre>

<p>Is there a better way of doing this?</p>

<p>The dictionaries are declared as follows:</p>

<pre><code>&lt;Application.Resources&gt;
    &lt;ResourceDictionary&gt;
        &lt;ResourceDictionary.MergedDictionaries&gt;
            &lt;ResourceDictionary Source="..." /&gt;
            &lt;ResourceDictionary Source="..." /&gt;
            ...
        &lt;/ResourceDictionary.MergedDictionaries&gt;
    &lt;/ResourceDictionary&gt;
&lt;/Application.Resources&gt;  
</code></pre>

<p>Actually, I'm programmatically opening flyouts declared in the merged dictionaries.</p>