c# - Exception while rendering XpsDocument after conversion from FlowDocument -


i'm developing application heavily uses conversion flowdocument xpsdocument. time time exception thrown: notsupportedexception: "the uri prefix not recognized."

my scenario complex , condensed simple test. if type in characters in text box (fast), you'll exception immediately.

xaml:

<window x:class="flowdocpreview.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <grid>         <grid.columndefinitions>             <columndefinition />             <columndefinition />         </grid.columndefinitions>         <richtextbox name="rtb" keyup="rtb_keyup" />         <documentviewer grid.column="1" name="dv" />     </grid> </window> 

code behind:

using system; using system.io; using system.io.packaging; using system.windows; using system.windows.documents; using system.windows.input; using system.windows.xps; using system.windows.xps.packaging;   namespace flowdocpreview {     public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();             rtb.document = new flowdocument();         }          uri _packageuri = null;          private void rtb_keyup(object sender, keyeventargs e)         {             using (memorystream ms = new memorystream())             {                 dv.document = null;                 if (_packageuri != null)                 {                     packagestore.removepackage(_packageuri);                 }                  _packageuri = new uri("memorystream://" + datetime.now.ticks + ".xps");                  textrange sourcecontent = new textrange(rtb.document.contentstart, rtb.document.contentend);                 memorystream stream = new memorystream();                 sourcecontent.save(stream, dataformats.xaml);                 flowdocument flowdocumentcopy = new flowdocument();                 textrange copydocumentrange = new textrange(flowdocumentcopy.contentstart, flowdocumentcopy.contentend);                 copydocumentrange.load(stream, dataformats.xaml);                  package package = package.open(ms, filemode.openorcreate, fileaccess.readwrite);                 packagestore.addpackage(_packageuri, package);                 xpsdocument xpsdoc = new xpsdocument(package, compressionoption.superfast);                 xpsdoc.uri = _packageuri;                 xpsdocumentwriter documentwriter = xpsdocument.createxpsdocumentwriter(xpsdoc);                 documentwriter.write(((idocumentpaginatorsource)flowdocumentcopy).documentpaginator);                  dv.document = xpsdoc.getfixeddocumentsequence();             }         }     } } 

that exception cannot catched (refer https://social.msdn.microsoft.com/forums/windows/en-us/431e8e80-3bf0-4679-a0c0-9b5cae4f2f38/systemnotsupportexception-the-uri-prefix-is-not-recognized-when-creating-xps-document?forum=netfxbcl ) or prevented in advance (the package seems existing correctly). examined .net code , found no reason strange behaviour. in total i've spent day or weeks find fix.

any ideas? regards, heady

finally got bearable solution. problem wpf application did not catch (any?) unhandled exceptions. start application in code , in xaml file had registered dispatcherunhandledexception handler. anyway did not catch. perhaps there additional steps ro initialize application before running it. added dispatcherunhandledexception handler in code, works me. can't avoid exception itself, @ least can handle it.

regards, headi


Comments