3

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;
    }
}
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
freakinpenguin
  • 831
  • 14
  • 24
  • Could you please describe your design and what exactly you cannot do like it is mentioned in the blog post? I don't get it at the moment. – MatthiasG Oct 05 '11 at 13:12

3 Answers3

9

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;
    }
}
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Thanks, that did the trick. Do you have an explanation for your solution? – freakinpenguin Oct 05 '11 at 13:45
  • Well the default `ShutdownMode` is `OnLastWindowClose`, so if you close all your child windows then it will close the application. `OnMainWindowClose` means the application will shutdown when your `App.Current.MainWindow` closes, and `OnExplicitShutdown` means it will only close when you tell it to close. – Rachel Oct 05 '11 at 13:51
  • So does the MainWindow not count as window when OnLastWindowClose? – freakinpenguin Oct 05 '11 at 13:55
  • @ThomasSpranger I'm not sure, but it probably depends on if the `MainWindow` is shown or not. – Rachel Oct 05 '11 at 14:12
  • Interesting thing. In my case it was visible all the time. Anyway - Thanks on more time! – freakinpenguin Oct 05 '11 at 14:27
1
  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();
            }
        }
Bhaskar
  • 49
  • 2
0

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.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Kingsley
  • 1
  • 1