title: accessing a resource dictionary in code wpf
body: <p>The same line of code in the same assembly works for one test fixture but not another. Here is the line of code:</p>
  
  <pre><code>var dic = new ResourceDictionary { Source = new Uri("pack://application:,,,/MyApp.Wpf;component/ImageResources.xaml") };
  </code></pre>
  
  <p>The error I get in the other test fixture is System.UriFormatException : Invalid URI: Invalid port specified.</p>
  
  <p>The uri string also works in xaml. Is there a better way to load a resource dictionary in code?</p>
  
  <p>Cheers,<br>
  Berryl</p>
  
  <p>=== UPDATE ===</p>
  
  <p>As I found in <a href="http://techfilth.blogspot.com/2010/01/dynamic-controltemplate-in-silverlight.html" rel="noreferrer">this posting</a>, an Invalid port was occurring because the pack scheme wasn't registered, which can be done with code like so:</p>
  
  <pre><code>if (!UriParser.IsKnownScheme("pack"))
       UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);
  </code></pre>
  
  <p>I am guessing that the test fixture that was able to load the dictionary with the pack scheme without error is because the SUT is a user control there, and is somehow loading resources when an instance of the user control is created.</p>