如何在应用程序初始化后立即打开对话框?
我试图在 WPF 应用程序启动后立即打开模式对话框(使用 ShowDialog(this)
)。我尝试了以下方法,这两种方法都抛出 InvalidOperationException,大概是因为窗口尚未初始化:
public MainWindow()
{
InitializeComponent();
ShowMyDialogDammit();
}
以及:
public MainWindow()
{
InitializeComponent();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
ShowMyDialogDammit();
}
我该如何执行此操作?
I'm trying to open a modal dialog as soon as a WPF application has started (using ShowDialog(this)
). I tried the following methods, both of which throw an InvalidOperationException, presumably because the window hasn't been initialized yet:
public MainWindow()
{
InitializeComponent();
ShowMyDialogDammit();
}
and:
public MainWindow()
{
InitializeComponent();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
ShowMyDialogDammit();
}
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为
FrameworkElement.Loaded
事件(“当元素被布局、渲染并准备好交互时”发生),然后从事件处理程序中打开对话框。
例如:
Add a handler for the
FrameworkElement.Loaded
event (which occurs "when the element is laid out, rendered, and ready for interaction"), and then open your dialog from within the event handler.For example:
尝试从窗口的 Loaded 事件中执行此操作。
Try doing it from the Loaded event of your windows.
试试这个:
您不需要在 MainWindow 类中的任何地方调用“ShowDialog”。
Try this:
You don't need the "ShowDialog" call inside the MainWindow class anywhere.