UriFormatException: invalid URI: invalid port specified

The line corresponding to the assembly, used as a parameter below for Uri to work in XAML, but gives me the error shown when used in the code.

I tried all kinds of UriKind with the same result. How can i fix this?

[Test] public void LargeImageSource_IsKnown() { var uri = new Uri( "pack://application:,,,/" + "MyAssembly.Core.Presentation.Wpf;component/" + "Images/Delete.png", UriKind.RelativeOrAbsolute); Assert.That( _pickerActivityCollectionVm.DeleteActivityCommand.LargeImageSource, Is.EqualTo(uri)); } System.UriFormatException : Invalid URI: Invalid port specified. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) at System.Uri..ctor(String uriString, UriKind uriKind) 

UPDATE

Based on Thomas's excellent answer and my own comments regarding readability, I ended up using the following in my BaseTestFixture class. Hope this helps someone else.

  protected virtual void OnFixtureSetUp() { // logging, other one time setup stuff... const string scheme = "pack"; if (!UriParser.IsKnownScheme(scheme)) { Assert.That(PackUriHelper.UriSchemePack, Is.EqualTo(scheme)); } } 
+46
uri resources wpf xaml
May 14 '11 at 23:20
source share
3 answers

This is because you are executing this code, and the pack:// scheme is not yet registered. This schema is registered when the Application object is created. You can add this code to the setup of your test device:

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



EDIT: it actually seems that the pack:// scheme is registered in the initializer of the PackUriHelper class PackUriHelper (which is used by the Application class). Thus, you do not need to create an instance of Application , you need to access the static PackUriHelper element to make sure that the type initializer is running:

 [SetUp] public void Setup() { string s = System.IO.Packaging.PackUriHelper.UriSchemePack; } 
+66
May 15 '11 at 12:05 a.m.
source share

It seems that access to PackUriHelper.UriSchemePack only registers the pack scheme, not the application scheme, in which I need to use the syntax pack://application:,,,/ in my unit tests. So I had to use the new Application() approach, which worked great for registering both schemas.

+9
Dec 14 '11 at 12:47
source share

If you see this error in the Windows Store / WinRT project:

I could not use the syntax "pack: //" at all when trying to load a resource in my C # application. What worked was the ms-appx: // syntax of this type:

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

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

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

Despite the fact that the question was asked by WPF, the problem seemed very similar, but in the end it turned out a completely different solution, which took some time to find, and the existing answers did not help at all.

Again, this solution does not apply to WPF

+7
Oct 04 '13 at 22:15
source share



All Articles