UriFormatException : Invalid URI: Invalid port specified UriFormatException : Invalid URI: Invalid port specified wpf wpf

UriFormatException : Invalid URI: Invalid port specified


That's because you're executing this code while the pack:// scheme is not yet registered. This scheme is registered when you create the Application object. You can add this code in the setup of your test fixture:

[SetUp]public void Setup(){    if (!UriParser.IsKnownScheme("pack"))        new System.Windows.Application();}

EDIT: actually it seems the pack:// scheme is registered in the type initializer of the PackUriHelper class (which happens to be used by the Application class). So actually you don't need to create an instance of Application, you only need to access a static member of PackUriHelper to ensure the type initializer has run:

[SetUp]public void Setup(){    string s = System.IO.Packaging.PackUriHelper.UriSchemePack;}


It appears that accessing PackUriHelper.UriSchemePack only registers the pack scheme, not the application scheme, which I needed to use the pack://application:,,,/ syntax in my unit tests. I therefore had to use the new Application() approach, which worked fine for registering both schemes.


If you're seeing this error in a Windows Store / WinRT project:

I wasn't able to use the "pack://" syntax at all when trying to load a resource in my C# app. What worked was ms-appx:// syntax of this kind:

ms-appx://[project folder]/[resource path]

For example, I wanted to load a resource dictionary named "styles.xaml" from a folder "core". This URI ended up working for me:

dictionary.Source = new System.Uri("ms-appx:///core/styles.xaml");

Even though the question specified WPF, the problem seemed extremely similar but ended up having a completely different solution, which took a while to find, and existing answers didn't help at all.

Again, this solution does not apply to WPF