UriFormatException : Invalid URI: Invalid port specified

后端 未结 3 1156
借酒劲吻你
借酒劲吻你 2020-12-23 16:01

The assembly qualified string used as a parameter below for a Uri works in XAML, but gives me the error shown when used in code.

I tried every kind of UriKind with

相关标签:
3条回答
  • 2020-12-23 16:31

    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.

    0 讨论(0)
  • 2020-12-23 16:36

    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;
    }
    
    0 讨论(0)
  • 2020-12-23 16:38

    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

    0 讨论(0)
提交回复
热议问题