WPF-应用程序启动-加载对话框窗口,然后加载主窗口。

5
我有一个WPF应用程序,并创建了一个登录窗口,用于构建应用程序的连接字符串。我遇到了关闭第一个对话框并旋转打开它后面的MainWindow的问题。我认为关闭事件从登录对话框冒泡出来并卡在MainWindow中,因为一旦我在codebehind中创建MainWindow对象并调用Show(),它就会直接通过我的Startup事件处理程序并进入我的构造函数然后进入MainWindow的onClosing处理程序,而不显示窗口本身。app.xaml指定了ShutdownMode =“OnMainWindowClose”。
private void Application_Startup(object sender, StartupEventArgs e)
    {
        try
        {
            Chooser thechooser = new Chooser();
            thechooser.ShowDialog();
        }
        catch (Exception ex)
        {

        }
        //initialize datalayer
        dataLayer = new Mxxx41.DAL(this.CurrentConnectionString);
        MainWindow appmainwindow = new MainWindow();
        Application.Current.MainWindow = appmainwindow;
        appmainwindow.Activate();
        appmainwindow.Show();
}

private void LogInButton_Click(object sender, RoutedEventArgs e)
    {
        //get ip from listbox selection
        XmlElement currentelement = (XmlElement)Listbox.SelectedItem;

        string ip = ((string)currentelement.Attributes["IP"].Value);
        string instancename = string.Empty;
        if (!((string)currentelement.Attributes["InstanceName"].Value == string.Empty))
        {
            instancename = ((string)currentelement.Attributes["InstanceName"].Value);
        }
        //ping that IP
        Boolean pingresult = ping.PingHost(ip);
        Boolean sqlresult = false;
        if (pingresult)
        {
            if (!(String.IsNullOrEmpty("instancename")))
            {
                ip = string.Format("{0}\\{1}", ip, instancename); 
            }

            //build connection string with that IP
            string connstr = BuildConnStr(ip);

            //create datalayer
            Mxxx41.DAL datalayer = new Mxxx41.DAL(connstr);
            //validate credentials
            DataSet data = datalayer.getDataSet("login_checkcredentials", CommandType.StoredProcedure, datalayer.CreateParameter("@username", SqlDbType.VarChar, this.UsernameTextbox.Text), datalayer.CreateParameter("@password", SqlDbType.VarChar, this.PasswordTextbox.Text));
            if (data.Tables[0].Rows.Count > 0)
            {
                sqlresult = true;

                //log in user
                //build new user code omitted for brevity


                App myAppReference = ((App)Application.Current);
                myAppReference.CurrentUser = thisuser;
                myAppReference.CurrentConnectionString = connstr;
                //close window
                this.Close();  //this is the close event I think is causing issues.
            }

        }
        else 
        { 
            ErrorLabel.Content = string.Format("{0}{1}", "could not ping selected Host :", ip); 
        }

        //return true


    }

public MainWindow(){
        this.InitializeComponent();

        this.SideBarExpander.IsExpanded = true;

        this.Loaded += onLoaded;
        this.Closed += onClosed;
        this.Closing += onClosing;

        try
        {
            //this.DataLayer = ((Mxxx41.DAL)MyDemoApp.App.Current.Properties["DataLayer"]);
            App myAppReference = ((App)Application.Current);
            this.DataLayer = myAppReference.GetDataLayer();
        }
        catch //catch everything for the moment
        {
            this.DataBaseConnectionError = true;
        }
        ExceptionList = new List<Error>();
    }

有人能帮我解决这个行为问题吗?
1个回答

4
问题可能出在ShutdownMode="OnMainWindowClose"。WPF认为第一个打开的窗口是“主窗口”。在您的情况下,WPF将您的登录窗口视为主窗口,并在其关闭时退出应用程序。
尝试将关闭模式更改为OnLastWindowCloseOnExplicitShutdown
来自MSDNOnMainWindowClose:当主窗口关闭或调用Shutdown时,应用程序将关闭。
OnExplicitShutdown:只有在调用Shutdown时,应用程序才会关闭。

谢谢 Zach。这是正确的。我没有意识到我的对话框窗口即使在几行下面重置了MainWindow对象,仍然会占用MainWindow引用。 - TWood
@TWood:不用谢。我认为令人困惑的可能是OnMainWindowClose并不是指MainWindow窗口关闭时触发,而是指第一个打开的窗口关闭时触发。 - Zach Johnson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接