0

如何以编程方式关闭 WPF PrintDialog?我试图通过Finalize反射来调用它,但这也没有关闭它。这是我尝试过的:

using System;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication15
{
    partial class Window1 : Window
    {
        PrintDialog _printDialog;

        public Window1()
        {
            InitializeComponent();
            new Thread(OpenDialog).Start();
            new Thread(CloseDialog).Start();
        }

        void OpenDialog()
        {
            Thread.Sleep(1000);
            Dispatcher.BeginInvoke((Action)OpenDialogImpl);
        }

        void OpenDialogImpl()
        {
            _printDialog = new PrintDialog();
            _printDialog.ShowDialog();
        }

        void CloseDialog()
        {
            Thread.Sleep(2000);
            Dispatcher.BeginInvoke((Action)CloseDialogImpl);
        }

        void CloseDialogImpl()
        {
            var type = typeof(PrintDialog);
            var flags = BindingFlags.Instance | BindingFlags.NonPublic;
            var finalize = type.GetMethod("Finalize", flags);
            finalize.Invoke(_printDialog, null);
            MessageBox.Show("Finalized");
        }
    }
}
4

1 回答 1

0

Internally, the PrintDialog class uses a Win32PrintDialog as a local variable to the ShowDialog() method, which in turn ends up uses the windows common dialog. Using reflection to get at something to close it might be futile, or at least maddening.

Just a stretch, as I haven't used it, but it might be possible to use White to issue a button press to the dialog's Cancel button. UISpy (mentioned on the White page) might be handy towards that end, too.

于 2010-02-18T21:30:56.607 回答