2

我有一个带有 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;
        }

当我启动此应用程序时,一切正常,直到我使用 DocumentViewer 切换到选项卡。应用程序崩溃,我得到这样的异常:

无法在只写模式下执行读取操作。

我做错了什么?是否可以进行此绑定?

4

1 回答 1

4

错误消息确实令人困惑,原因也不是很明显。基本上你关闭的MemoryStream太早XpsDocument了,当DocumentViewer尝试读取文档时它不能,因为它是只写模式(因为流已关闭)。

解决方案是在您完成查看文档MemoryStream之前不要立即关闭。为了实现这一点,我写了一个that returns 。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;
        }
    }

}

XpsReference工具IDisposable所以记得调用Dispose()它。

此外,一旦您解决了上述错误,您可能会遇到的下一个问题将是内容未按预期呈现。这是因为您需要克隆FlowDocument并且它没有经过全面测量和安排布局传递。阅读 将 BlockUIContainer 打印到 XpsDocument/FixedDocument以了解如何解决此问题。

于 2012-03-11T08:45:22.287 回答