开发者

Viewing an XPS document with malformed URI's in WPF

开发者 https://www.devze.com 2023-02-06 12:15 出处:网络
I\'m trying to use DocumentViewer (or, more specifically, DocumentViewer\'s DocumentPageView) to load a presentation that was saved from Powerpoint as XPS.

I'm trying to use DocumentViewer (or, more specifically, DocumentViewer's DocumentPageView) to load a presentation that was saved from Powerpoint as XPS.

However, the author of the slides was being clever and entered one of his URLs as a pseudo-regex (e.g. http://[blog|www]mywebsite.com). The built in XPS Viewer is able to load the document without a problem. However, DocumentViewer throws an exception because it tries to validate the URI:

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

I could of course go into the slide and fix the URI so tha开发者_如何学运维t the document displays. However, since I can't control the documents that will be used with my application, I'd prefer to find a way to display the document in spite of invalid URI's (like XPS Viewer).

Any thoughts?


The DocumentViewer is trying to create a Uri instance from the provided URL. If the URL isn't valid, the operation will fail.

You can prevent this from happening by performing validation on the URLs provided to you by the author. (writing this without testing, so there may be some syntax errors)

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号