【问题标题】:Viewing an XPS document with malformed URI's in WPF在 WPF 中查看格式错误的 URI 的 XPS 文档
【发布时间】:2011-06-15 09:00:24
【问题描述】:

我正在尝试使用 DocumentViewer(或者,更具体地说,DocumentViewer 的 DocumentPageView)来加载从 Powerpoint 保存为 XPS 的演示文稿。

但是,幻灯片的作者很聪明,将他的其中一个 URL 输入为伪正则表达式(例如 http://[blog|www]mywebsite.com)。内置的 XPS 查看器能够毫无问题地加载文档。但是,DocumentViewer 会抛出异常,因为它会尝试验证 URI:

Failed to create a 'NavigateUri' from the text 'http://[blog|www]mywebsite.com'

我当然可以进入幻灯片并修复 URI 以便显示文档。但是,由于我无法控制将与我的应用程序一起使用的文档,因此我更愿意找到一种方法来显示文档,尽管 URI 无效(例如 XPS 查看器)。

有什么想法吗?

【问题讨论】:

    标签: wpf xps xpsdocument


    【解决方案1】:

    DocumentViewer 正在尝试从提供的 URL 创建一个 Uri 实例。如果 URL 无效,则操作将失败。

    您可以通过对作者提供给您的 URL 进行验证来防止这种情况发生。 (写这个没有测试,所以可能会有一些语法错误)

    public static bool IsValidUrl(this string url)
    {
      if(string.IsNullOrWhitespace(url) return false;
      try
      {
         var uri = new Url(url);
         return true;
      }
      catch
      {
        // if you were implementing IDataErrorInfo rather than using a
        // lousy extension method you would catch the exception
        // here and display it to the user
        return false;
      } 
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-04
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2011-02-17
      • 1970-01-01
      • 2017-11-18
      相关资源
      最近更新 更多