title: How can I programmatically access a resource string from App.xaml?
body: <p>I have some Strings I want to access from my code in App.xaml like:</p>
  
  <pre><code>&lt;Application.Resources&gt;
      &lt;ai:TelemetryContext x:Key="ApplicationInsightsBootstrapper" xmlns:ai="using:Microsoft.ApplicationInsights"/&gt;
      &lt;ResourceDictionary x:Key="rd"&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="GlobalStylesResourceDictionary.xaml"/&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;!-- Application-specific resources. --&gt;
          &lt;x:String x:Key="AppName"&gt;Platypus&lt;/x:String&gt;
          &lt;x:String x:Key="BingMapsNamespace"&gt;http://schemas.microsoft.com/search/local/ws/rest/v1&lt;/x:String&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p>And I want to programmatically get those values, so I wrote this utility function:</p>
  
  <pre><code>internal static String GetResourceString(String keyStr)
  {
      var resldr = new Windows.ApplicationModel.Resources.ResourceLoader();
      return resldr.GetString(keyStr);
  }
  </code></pre>
  
  <p>But if fails with, "<em>A first chance exception of type 'System.Exception' occurred in Platypus.exe WinRT information: ResourceMap Not Found.</em>"</p>
  
  <p>I thought I found the solution to the problem <a href="https://stackoverflow.com/questions/10800730/resourcemap-not-found-error-when-referencing-a-%20resource-file-within-a-portable-c">here</a>, where it is said that way of doing it is deprecated, and I should do this instead:</p>
  
  <pre><code>internal static String GetResourceString(String keyStr)
  {
      try
      {
          //var resldr = new Windows.ApplicationModel.Resources.ResourceLoader();
          //return resldr.GetString(keyStr);
          var resldr = ResourceLoader.GetForCurrentView();
          return resldr.GetString(keyStr);
      }
      catch (Exception ex)
      {
          Debug.WriteLine("Something wrong with the keyStr? Exception in GetResourceString(): {0}", ex.Message); 
      }
      return String.Empty;
  }
  </code></pre>
  
  <p>But that doesn't work, either; I get the same old "WinRT information: ResourceMap Not Found." with this code, too.</p>
  
  <p>The arg I'm passing is valid:</p>
  
  <pre><code>String bmn = GetResourceString("BingMapsNamespace");
  </code></pre>
  
  <p>So what's the dealio?</p>
  
  <h2>UPDATE</h2>
  
  <p>It may be just as well (or better, actually, as it will presumably work), to use App.xaml.cs rather than its angly cousin:</p>
  
  <pre><code>public static string BingMapsNamespace { get; set; }
  . . .
  BingMapsNamespace = "http://schemas.microsoft.com/search/local/ws/rest/v1";
  </code></pre>
  
  <p>And then access it from elsewhere like so:</p>
  
  <pre><code>String bmn = App.BingMapsNamespace;
  </code></pre>
  
  <p>Later: And yes, it does work. I'm marking the other as correct, as it obviously works for some people.</p>