如何获取使用的uri但抛出UriFormatException?

时间:2016-08-04 17:33:23

标签: c# webview exception-handling uri uwp

我正在使用webview,并且一度导航到URi Scheme,我订阅了一个名为UnsupportedUriSchemeIdentified的事件。

但是当我检查它的事件参数时,它说无法解析uri,如何获取它试图解析的uri enter image description here

1 个答案:

答案 0 :(得分:0)

由于UWP的源代码没有浮动,我无法访问sdk,我将展示一种方法,而不是直接提供解决方案。

我认为EventArgs看起来有点像这样:

public sealed class SomeEventArgs : EventArgs
{
    private readonly string address;
    public Uri Uri => new Uri(address);

    public SomeEventArgs(string address)
    {
        this.address = address;
    }
}
// If the address is not formatted property, 
// trying to get the `Uri` will cause an exception to be thrown: 
var e = new SomeEventArgs("qwe'1231[e);");

// But, we can use to reflection to find out what private fields are,
// and possibly dug out something interesting
var fields = typeof(SomeEventArgs).GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
    .Select(x => new { x.Name, x.FieldType });

// Say if you find something interesting, like 'address',
// you can retrieve its value like this
var address = typeof(SomeEventArgs).GetField("address", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(e);