WPF:如何以编程方式关闭 PrintDialog?
如何以编程方式关闭 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");
}
}
}
How can I programatically close a WPF PrintDialog
? I tried 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");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在内部,
PrintDialog
类使用Win32PrintDialog
作为ShowDialog()
方法的局部变量,该方法最终使用 Windows 公共对话框。使用反射来获取某些东西来关闭它可能是徒劳的,或者至少是令人抓狂的。只是一个延伸,因为我还没有使用过它,但也许可以使用 White 来发出按下对话框的取消按钮。 UISpy(在白皮书中提到)也可能有助于实现这一目标。
Internally, the
PrintDialog
class uses aWin32PrintDialog
as a local variable to theShowDialog()
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.