WPF打印预览工作正常,但对话框保持打开状态

问题描述 投票:0回答:1

我主要使用Ray的答案中的代码How to Print Preview when using a DocumentPaginator to print?以使用DocumentViewer显示打印预览(感谢Ray)。它可以正常工作,我可以从窗口打印,但是它保持打开状态。在我手动关闭窗口之前,ShowDialog不会返回true或其他任何值。有什么想法吗?这是代码:

        string tempFileName = System.IO.Path.GetTempFileName();
        File.Delete(tempFileName);
        using (XpsDocument xpsDocument = new XpsDocument(tempFileName, 
    FileAccess.ReadWrite))
        {
            XpsDocumentWriter writer = 
    XpsDocument.CreateXpsDocumentWriter(xpsDocument);
            writer.Write(doc);

            PrintPreview previewWindow = new PrintPreview
            {
                Owner = this,
                Document = xpsDocument.GetFixedDocumentSequence()
            };

            bool result = (bool)previewWindow.ShowDialog();
            if (result == true)
             // it never is....

        public partial class PrintPreview : Window
        {
            public PrintPreview()
            {
                InitializeComponent();
            }
            public IDocumentPaginatorSource Document
            {
                get { return viewer.Document; }
                set { viewer.Document = value; }
            }
         }

XAML:

    <Window x:Class="MyNamespace.PrintPreview"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="previewWindow"
    Title="Print Preview" Height="800" Width="800">
    <Grid>
        <DocumentViewer Name="viewer" 
              Document="{Binding ElementName=previewWindow, Path=Document}" />
    </Grid>
    </Window>
wpf printing xps print-preview documentviewer
1个回答
0
投票

我认为您可以在窗口中添加“打印”和“取消”按钮,单击“打印”按钮时,关闭窗口并返回true,当单击“取消”按钮或用户关闭窗口时,关闭窗口并返回false。

因为我看到了您的代码,但没有找到任何代码来关闭窗口(我想您忘记了。)>

此外,我想您可以尝试以下方法:The Custom Print Dialog With Preview in Real Time

© www.soinside.com 2019 - 2024. All rights reserved.