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まですぐに閉じないことです。XpsDocumentConverterこれを達成するために、を返すを書きましたXpsReference

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 に答える