Art*_*luk 2 wpf binding richtextbox documentviewer flowdocument
我有一个带有 RichTextBox 和 DocumentViewer 的应用程序(放置在 TabControl 中),我想制作类似“热预览”的内容。我已将DocumentViewer.Document财产绑定到RichTextBox.Document
捆绑:
<DocumentViewer Document="{Binding Document, Converter={StaticResource FlowDocumentToPaginatorConverter}, ElementName=mainRTB, Mode=OneWay}" />
这是转换器代码:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
FlowDocument d = value as FlowDocument;
DocumentPaginator pagin = ((IDocumentPaginatorSource)d).DocumentPaginator;
FixedDocumentSequence result = null;
Size s = new Size(793.700787402, 1122.519685039);
pagin.PageSize = s;
using (MemoryStream ms = new MemoryStream())
{
TextRange tr = new TextRange(d.ContentStart, d.ContentEnd);
tr.Save(ms, DataFormats.XamlPackage);
Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri uri = new Uri(@"memorystream://doc.xps");
PackageStore.AddPackage(uri, p);
XpsDocument xpsDoc = new XpsDocument(p);
xpsDoc.Uri = uri;
XpsDocument.CreateXpsDocumentWriter(xpsDoc).Write(pagin);
result = xpsDoc.GetFixedDocumentSequence();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
当我启动此应用程序时,一切正常,直到我使用 DocumentViewer 切换到选项卡。应用程序崩溃了,我得到这样的异常:
无法在只写模式下执行读取操作。
我做错了什么?是否可以进行此绑定?
错误消息确实令人困惑,原因也不是很明显。基本上,您关闭的内容MemoryStream太早了XpsDocument,当DocumentViewer尝试读取文档时,它不能,因为它是只写模式(因为流已关闭)。
解决方案是在查看完文档MemoryStream之前不要立即关闭。为了实现这一点,我写了一个返回的.XpsDocumentConverterXpsReference
此外,由于您从未能够转换和显示单个包,因此XpsDocument您不会遇到下一个问题,即在PackageStore同一Uri. 我在下面的实现中已经解决了这个问题。
public static XpsDocumentReference CreateXpsDocument(FlowDocument document)
{
// Do not close the memory stream as it still being used, it will be closed
// later when the XpsDocumentReference is Disposed.
MemoryStream ms = new MemoryStream();
// We store the package in the PackageStore
Uri uri = new Uri(String.Format("pack://temp_{0}.xps/", Guid.NewGuid().ToString("N")));
Package pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
PackageStore.AddPackage(uri, pkg);
XpsDocument xpsDocument = new XpsDocument(pkg, CompressionOption.Normal, uri.AbsoluteUri);
// Need to force render the FlowDocument before pagination.
// HACK: This is done by *briefly* showing the document.
DocumentHelper.ForceRenderFlowDocument(document);
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDocument), false);
DocumentPaginator paginator = new FixedDocumentPaginator(document, A4PageDefinition.Default);
rsm.SaveAsXaml(paginator);
return new XpsDocumentReference(ms, xpsDocument);
}
public class XpsDocumentReference : IDisposable
{
private MemoryStream MemoryStream;
public XpsDocument XpsDocument { get; private set; }
public FixedDocument FixedDocument { get; private set; }
public XpsDocumentReference(MemoryStream ms, XpsDocument xpsDocument)
{
MemoryStream = ms;
XpsDocument = xpsDocument;
DocumentReference reference = xpsDocument.GetFixedDocumentSequence().References.FirstOrDefault();
if (reference != null)
FixedDocument = reference.GetDocument(false);
}
public void Dispose()
{
Package pkg = PackageStore.GetPackage(XpsDocument.Uri);
if (pkg != null)
{
pkg.Close();
PackageStore.RemovePackage(XpsDocument.Uri);
}
if (MemoryStream != null)
{
MemoryStream.Dispose();
MemoryStream = null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
XpsReference实现IDisposable所以记得调用Dispose()它。
此外,一旦解决了上述错误,您可能遇到的下一个问题将是内容未按您的预期呈现。这是因为您需要克隆FlowDocument并且它没有经过完整的测量和安排布局过程。请阅读
将 BlockUIContainer 打印到 XpsDocument/FixedDocument了解如何解决此问题。