【问题标题】:How to get the uri which was used but threw UriFormatException?如何获取已使用但抛出 UriFormatException 的 uri?
【发布时间】:2016-12-10 23:09:29
【问题描述】:

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

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

【问题讨论】:

  • 如果都失败了,你可以随时尝试使用Reflection来检索用于生成Uri的私有字段。
  • 访问 Uri 属性时抛出 UriFormatException :(
  • 不要观察物业。尝试找到用于创建Uri 的字段。
  • 能否请您发布您的 URI,以便我进行测试?
  • @GraceFeng-MSFT 它看起来像这样:ticketurlscheme://{"status":"FAILED", "transaction_id":"asdasdasdasdasd", "result":"FAILED"} ....因为这个方案无效(因为它是 json)它会抛出异常,但我需要得到这个字符串来自己解析 json 但我无法得到它

标签: c# webview exception-handling uri uwp


【解决方案1】:

由于没有 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);

【讨论】:

  • 它有 0 个私有字段字段,我认为这是因为异常:(
猜你喜欢
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 2018-03-20
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多