如何在应用程序初始化后立即打开对话框?

发布于 2024-10-08 05:55:10 字数 445 浏览 0 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪 2024-10-15 05:55:10

FrameworkElement.Loaded事件(“当元素被布局、渲染并准备好交互时”发生),然后从事件处理程序中打开对话框。
例如:

public MainWindow()
{
    InitializeComponent();

    // Adding the event handler
    Loaded += new RoutedEventHandler(IsLoaded);
}

private void Loaded(object sender, RoutedEventArgs e)
{
    ShowMyDialogDammit();
}

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:

public MainWindow()
{
    InitializeComponent();

    // Adding the event handler
    Loaded += new RoutedEventHandler(IsLoaded);
}

private void Loaded(object sender, RoutedEventArgs e)
{
    ShowMyDialogDammit();
}
魂归处 2024-10-15 05:55:10

尝试从窗口的 Loaded 事件中执行此操作。

Try doing it from the Loaded event of your windows.

春夜浅 2024-10-15 05:55:10

试试这个:

var w = new MainWindow();
w.ShowDialog();

您不需要在 MainWindow 类中的任何地方调用“ShowDialog”。

Try this:

var w = new MainWindow();
w.ShowDialog();

You don't need the "ShowDialog" call inside the MainWindow class anywhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文