Tôi có một ứng dụng wpf và tôi đã tạo một cửa sổ đăng nhập được sử dụng để xây dựng chuỗi kết nối của ứng dụng. Tôi gặp sự cố khi đóng hộp thoại đầu tiên và quay vòng MainWindow phía sau. Tôi nghĩ rằng một sự kiện gần như là bong bóng ra khỏi hộp thoại đăng nhập và bị kẹt trong MainWindow vì ngay sau khi tôi tạo đối tượng MainWindow trong codebehind và gọi Show() nó chỉ di chuyển ngay qua trình xử lý sự kiện khởi động của tôi và vào hàm khởi tạo của tôi onClosing các trình xử lý của MainWindow mà không bao giờ hiển thị cửa sổ. Các app.xaml có ShutdownMode = "OnMainWindowClose" quy định.wpf- Application_Startup - cửa sổ tải hộp thoại THEN mainwindow
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>();
}
Ai đó có thể giúp tôi với hành vi này?
Cảm ơn Zach. Điều này là chính xác. Tôi không hiểu rằng cửa sổ hộp thoại của tôi đã đánh cắp tham chiếu MainWindow mặc dù tôi đã đặt lại đối tượng MainWindow một vài dòng bên dưới. – TWood
@TWood: Bạn được chào đón. Tôi nghĩ rằng điều gây nhầm lẫn có lẽ là 'OnMainWindowClose' không có nghĩa là khi cửa sổ 'MainWindow' đóng lại, nhưng khi cửa sổ đầu tiên mở ra. –