我正在開發大量使用從FlowDocument到XpsDocument的轉換的應用程序。有時會拋出異常: NotSupportedException:「無法識別URI前綴。」在從FlowDocument轉換後呈現XpsDocument時出現異常
我的場景非常複雜,我把它簡化爲一個簡單的測試。如果你在文本框中鍵入了一些字符(快),你會立即得到異常。
的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>
代碼後面:
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();
}
}
}
}
即異常不能被獲取(參照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)或預先防止(包似乎是正確現有的)。我檢查了.net代碼,發現沒有理由爲這種奇怪的行爲。總的來說,我花了一天甚至幾周的時間來尋找解決方法。
任何想法?問候,興奮
當你輸入速度快是否只發生? –
在該例中是。但即使每分鐘使用一次,它也不會發生。該應用程序的使用效率很高,我們每週都會收到一次錯誤消息。 – Headi