查看URI格式错误的XPS文档';WPF中的s
我正在尝试使用DocumentViewer(或者更具体地说,DocumentViewer的DocumentPageView)加载从Powerpoint保存为XPS的演示文稿 然而,幻灯片的作者很聪明,以伪正则表达式的形式输入了他的一个URL(例如查看URI格式错误的XPS文档';WPF中的s,wpf,xps,xpsdocument,Wpf,Xps,Xpsdocument,我正在尝试使用DocumentViewer(或者更具体地说,DocumentViewer的DocumentPageView)加载从Powerpoint保存为XPS的演示文稿 然而,幻灯片的作者很聪明,以伪正则表达式的形式输入了他的一个URL(例如http://[blog | www]mywebsite.com)。内置的XPS查看器能够毫无问题地加载文档。但是,DocumentViewer会引发异常,因为它尝试验证URI: Failed to create a 'NavigateUri' from
http://[blog | www]mywebsite.com
)。内置的XPS查看器能够毫无问题地加载文档。但是,DocumentViewer会引发异常,因为它尝试验证URI:
Failed to create a 'NavigateUri' from the text 'http://[blog|www]mywebsite.com'
当然,我可以进入幻灯片并修复URI,以便显示文档。但是,由于我无法控制将与应用程序一起使用的文档,因此我更希望找到一种方法来显示文档,尽管URI无效(如XPS查看器)
有什么想法吗?DocumentViewer正在尝试从提供的URL创建实例。如果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;
}
}