gpt4 book ai didi

wpf- Application_Startup-加载对话框窗口然后进入主窗口

转载 作者:行者123 更新时间:2023-12-04 20:23:27 34 4
gpt4 key购买 nike

我有一个wpf应用程序,并创建了一个登录窗口,该窗口用于构建应用程序的连接字符串。我在关闭第一个对话框并在其后面旋转打开MainWindow时遇到问题。我认为close事件正在从登录对话框中冒出,并卡在MainWindow中,因为一旦我在代码背后创建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>();
}

有人可以帮我解决这种问题吗?

最佳答案

问题可能出在ShutdownMode="OnMainWindowClose"上。 Wpf认为打开的第一个窗口是“主窗口”。在您的情况下,wpf将登录窗口视为主窗口,并在关闭时退出应用程序。

尝试将关闭模式更改为OnLastWindowCloseOnExplicitShutdown

MSDN:

OnMainWindowClose :当主窗口关闭或调用Shutdown时,应用程序关闭。
OnExplicitShutdown :仅当调用Shutdown时,应用程序才会关闭。

关于wpf- Application_Startup-加载对话框窗口然后进入主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3738032/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com