How can I programatically close a WPF PrintDialog
? I tri开发者_运维技巧ed to call Finalize
on it trough reflection, and that does not close it either. Here is what I tried with:
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");
}
}
}
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.
精彩评论