WPF 关闭子窗口 - 关闭父窗口

发布于 2024-12-08 10:33:20 字数 1215 浏览 5 评论 0原文

我有与提到的完全相同的示例 此处。 快速得出结论:当最后一个子窗口关闭时,主窗口也关闭。

我的问题:我无法使用所描述的解决方案解决我的问题。我无法制作一个发生这种情况的程序。仅在我的一项较大的项目中。也许有人有想法或知道任何进一步的步骤。

感谢您的阅读 - Thomas

根据要求,这里有一些代码: 这是主窗口中的部分:

bool editAfterSearch = false;
Movie selectedMovie = (Movie)this.listView.SelectedItem;
Movie backup = (Movie)selectedMovie.Clone();            

if (new OnlineSearchWindow().EditMovieViaOnlineSearch(ref selectedMovie, out editAfterSearch))
{
    this.coverFlow.Update(selectedMovie);
}

这是子窗口中的部分:

public bool EditMovieViaOnlineSearch(ref Movie preset, out bool editAfter)
{
    this.exitWithOk = false;
    this.editMovieAfterSearch = false;

    this.tbx_SearchTerm.Text = preset.Title;
    this.linkedMovie = preset;

    this.ShowDialog();

    editAfter = editMovieAfterSearch;

    if (this.exitWithOk)
    {
        this.linkedMovie.CloneOnlineInformation(ref preset);
        preset.Bitmap = this.linkedMovie.Bitmap;

        return true;
    }
    else
    {
        return false;
    }
}

i have the pretty same sample as mentioned here.
Fast concluded: MainWindow closes when the last childwindow is closed.

My Problem: I couldn't solve my problems with the described solutions. I can't produce a program where it als takes place. Only in one of my bigger progs. Maybe someone has an idea or knows any further steps.

Thanks for reading - Thomas

As requested here's a bit of code:
This is the part in the MainWindow:

bool editAfterSearch = false;
Movie selectedMovie = (Movie)this.listView.SelectedItem;
Movie backup = (Movie)selectedMovie.Clone();            

if (new OnlineSearchWindow().EditMovieViaOnlineSearch(ref selectedMovie, out editAfterSearch))
{
    this.coverFlow.Update(selectedMovie);
}

And that's the part of the ChildWindow:

public bool EditMovieViaOnlineSearch(ref Movie preset, out bool editAfter)
{
    this.exitWithOk = false;
    this.editMovieAfterSearch = false;

    this.tbx_SearchTerm.Text = preset.Title;
    this.linkedMovie = preset;

    this.ShowDialog();

    editAfter = editMovieAfterSearch;

    if (this.exitWithOk)
    {
        this.linkedMovie.CloneOnlineInformation(ref preset);
        preset.Bitmap = this.linkedMovie.Bitmap;

        return true;
    }
    else
    {
        return false;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

那片花海 2024-12-15 10:33:20

尝试使用 App.xaml.csShutDownMode 属性。这 3 个值分别是 OnMainWindowCloseOnLastWindowCloseOnExplicitShutdown,默认为 OnLastWindowClose

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
    }
}

Try playing with the ShutDownMode property of your App.xaml.cs. The 3 values are OnMainWindowClose, OnLastWindowClose, and OnExplicitShutdown, and the default is OnLastWindowClose

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
    }
}
旧瑾黎汐 2024-12-15 10:33:20
  1. 下面的代码对我有用。
 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBoxResult.No == (MessageBox.Show("Are you sure you want to close this?.", "ProjectName", MessageBoxButton.YesNo)))
            {
                e.Cancel = true;
                foreach (var item in Application.Current.Windows)
                {
                    Window window = item as Window;
                    if (window.Title == "PopUpWindowName")
                    {
                        window.Topmost = true;
                        break;
                    }
                }
                return;
            }
            else
            {
                base.OnClosed(e);
                Application.Current.Shutdown();
            }
        }
  1. The below code worked for me.
 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBoxResult.No == (MessageBox.Show("Are you sure you want to close this?.", "ProjectName", MessageBoxButton.YesNo)))
            {
                e.Cancel = true;
                foreach (var item in Application.Current.Windows)
                {
                    Window window = item as Window;
                    if (window.Title == "PopUpWindowName")
                    {
                        window.Topmost = true;
                        break;
                    }
                }
                return;
            }
            else
            {
                base.OnClosed(e);
                Application.Current.Shutdown();
            }
        }
拔了角的鹿 2024-12-15 10:33:20

您可以尝试将子窗口的 allowShutDown 设置为 false,然后显示主窗口。我假设您首先将主窗口的可见性设置为隐藏

Application.Current.MainWindow.Visibility = System.Windows.Visibility.Visible;
this.allowShutDown = false;  

allowShutDown 将是您自己的属性,您可以设置该属性以使您必须处理opening 事件。

you can try setting the child window's allowShutDown to false and then show the mainwindow. I'm assuming you will start with mainwindow's visibility set to hidden.

Application.Current.MainWindow.Visibility = System.Windows.Visibility.Visible;
this.allowShutDown = false;  

The allowShutDown will be your own property which u can set to enable you have to handle the closing event.

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