samedi 9 mai 2015

Clarification on WPF Window.Show() and Window.Close() and how they work with Multiple Windows

The issue I am having and looks like many people have had in the past when dealing with WPF is I want to either hide or close the MainWindow of the application when a new Window is open. The reason I want to do this is because making tabs for it doesn't look neat to me, especially with how I designed my UI. I have tried using newWindow.Show() and oldWindow.Close(), however when the program runs, the oldWindow is still being shown when it should have been closed. I have also tried making it so the oldWindow is hidden when the newWindow has been called. They both produce the exact same results. As I understand it, You cannot actually close the MainWindow because it is needed for the program to continue to run. So, I know I want to go the route of hiding the MainWindow, but as you can see with my question, it isn't working as intended. Can anyone possibly explain what I am doing wrong or give pointers for an

alternative method I could go with?

namespace WorkinProgress
        {
            /// <summary>
            /// Interaction logic for EnglishLanguageSelection.xaml
            /// </summary>
            public partial class EnglishLanguageSelection : Page
            {
                public EnglishLanguageSelection()
                {
                    InitializeComponent();
                    loadList();
                }

                private void loadList()
                {
                    listBox.Items.Add("math");
                    listBox.Items.Add("science");
                    listBox.Items.Add("history");
                    listBox.Items.Add("english");
                }

                private void listBox_SelectionChanged(object sender, 

SelectionChangedEventArgs e)
                {
                    if (listBox.SelectedIndex == 1)
                    {
            MainWindow main = new MainWindow();
                        ScienceWindow sci = new ScienceWindow();
                        NavigationService.Navigate(sci);
                        main.Close();
                    }
                }
            }
        }

I have also tried changing the event handler to:

    private void listBox_SelectionChanged(object sender, 

SelectionChangedEventArgs e)
    {
        if (listBox.SelectedIndex == 1)
        {
            MainWindow main = new MainWindow();
            ScienceWindow sci = new ScienceWindow();
            sci.Show();
            main.Visibility = System.Windows.Visibility.Hidden;
        }
    }

Aucun commentaire:

Enregistrer un commentaire