UriFormatException:无效的 URI:指定的端口无效 [英] UriFormatException : Invalid URI: Invalid port specified

查看:186
本文介绍了UriFormatException:无效的 URI:指定的端口无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用作下面 Uri 参数的程序集限定字符串在 XAML 中工作,但在代码中使用时给我显示的错误.

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.

我尝试了各种 UriKind,结果都一样.我该如何解决这个问题?

I tried every kind 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)

更新

基于 Thomas 出色的回答和我自己对可读性的评论,我最终在 BaseTestFixture 类中使用了以下内容.希望这对其他人有帮助.

UPDATE

Based on Thomas' superb answer and my own comments about readability, I wound 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));
        }
    }

推荐答案

那是因为您正在执行此代码而 pack:// 方案尚未注册.该方案在您创建 Application 对象时注册.您可以在测试装置的设置中添加此代码:

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();
}

<小时>

实际上 pack:// 方案似乎注册在 PackUriHelper 类的类型初始值设定项中(恰好由 Application 类).所以实际上你不需要创建Application的实例,你只需要访问PackUriHelper的一个静态成员来确保类型初始化器已经运行:


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;
}

这篇关于UriFormatException:无效的 URI:指定的端口无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆