WPF XPS文档不再显示图像

nx7onnlm  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(177)

我使用下面的代码很多年了,但是突然停止显示图像。我做了一个简短的版本来测试它并解释:
有一个UserControlXamlPage,它只包含两个控件:

<StackPanel>
    <TextBlock Text="My Content"/>
    <Image Source="/Images/flower.jpg" Height="100"/>
</StackPanel>

XamlPage用于DocumentPaginator类的派生类中:

public class Paginator : DocumentPaginator
{
    double pageHeight = 1122.52;
    double pageWidth = 793.7;
    Size mySize;

    public Paginator()
    {
        mySize = new Size(pageWidth, pageHeight);
    }

    
    public override DocumentPage GetPage(int pageNumber)
    {
        XamlPage page = new XamlPage();
        page.Measure(PageSize);
        page.Arrange(new Rect(new Point(0, 0), PageSize));
        page.UpdateLayout();
        return new DocumentPage(page, PageSize, new Rect(0, 0, 10, 10), new Rect());
    }

    public override bool IsPageCountValid
    {
        get { return true; }
    }

    public override int PageCount
    {
        get { return 1; }
    }

    public override Size PageSize
    {
        get { return mySize; }
        set { mySize = value; }
    }

    public override IDocumentPaginatorSource Source
    {
        get { return null; }
    }
}

在主窗口中,只有两个按钮,一个打开使用XamlPage控件的新窗口,另一个打开Preview窗口。按钮绑定到MainVM中的Commands

private void PreviewCommandAction()
{
    Paginator paginator = new Paginator();

    MemoryStream lMemoryStream = new MemoryStream();
    Package package = Package.Open(lMemoryStream, FileMode.Create);
    XpsDocument xpsDocument = new XpsDocument(package);
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
    writer.Write(paginator);
    xpsDocument.Close();
    package.Close();

    Preview previewWindow = new Preview(lMemoryStream);

    previewWindow.ShowDialog();
}

private void WindowCommandCommandAction()
{
    XamlWindow xw = new XamlWindow();
    xw.Show();
}

Preview窗口仅包含一个控件:

<DocumentViewer Document="{Binding Document}"/>

PreviewVM

public class PreviewVM : ObservableObject
{
    private IDocumentPaginatorSource myDocument;
    private XpsDocument xps;
    private Uri packageUri;
    private Stream docStream;

    public PreviewVM(MemoryStream xpsStreamDocument)
    {
        docStream = xpsStreamDocument;
        Package package = Package.Open(docStream);

        //Create URI for Xps Package
        //Any Uri will actually be fine here. It acts as a place holder for the
        //Uri of the package inside of the PackageStore
        string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid());
        packageUri = new Uri(inMemoryPackageName);

        //Add package to PackageStore
        PackageStore.AddPackage(packageUri, package);

        xps = new XpsDocument(package, CompressionOption.SuperFast, inMemoryPackageName);
        Document = xps.GetFixedDocumentSequence();
    }

    public IDocumentPaginatorSource Document
    {
        get { return myDocument; }
        set
        { 
            if (myDocument == value) return;
            myDocument = value;
            OnPropertyChanged(nameof(Document));
        }
    }
}

因此,在MainVM中,WindowCommandCommandAction方法打开一个新窗口并显示UserControlXamlPage,没有任何意外。但PreviewCommandAction方法正确打开Preview窗口,此预览仅显示TextBox的内容,而不显示Image。没有错误消息,Windows事件查看器中没有任何内容。我再说一遍,直到两天前,这一直在工作。
顺便问一下,有没有其他的解决方案来获得预览?我需要使用DocumentPaginator来管理页面。
整个测试项目位于此存储库中:
https://github.com/yannicklal/TestPreview

相关问题