-2
我在WPF窗口的Window_loaded子窗口中通过某些代码打开时做了一些检查。如何让用户从其window_loaded事件中关闭wpf窗口
在某些情况下,我需要通过messagebox.show给用户一个选择并关闭窗口。
If MessageBox.Show("Question the user?",
"Ask", MessageBoxButton.OKCancel, MessageBoxImage.Question) = MessageBoxResult.OK Then
'do some further code
Else
Me.Close()
End If
当我这样做时,当Messagebox出现时,wpf窗口显示为黑色窗口。
问题1:我怎样才能在MessageBox而不显示WPF窗口为一个黑盒子背后
这里是在load_window事件的完整代码。
'Initialise window when Load event flagged
'The window expects to called after a Status variable is set to the newly created window object
'status=1 then it attempts to load the current activity
'status=2 then it checks the user has a current activity, offers to a)stop and save it and then open a new activity Or b) close.
'status=other, shouldn't happen, theres an error close the application
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
log.logInDB(clsLoggedInUser.login, "CurrentActivityWindow", "Info", "Form Loading. Status" & status)
Select Case Me.status
Case 1 'Current Activity
'Ask the Activity to load details for the users current Activity
Debug.Print("Window loaded - current Activity")
myActivity.GetCurrentActivityForUser(clsLoggedInUser.login)
Case 2 'New Activity
Debug.Print("Window loaded - new Activity")
myActivity.GetCurrentActivityForUser(clsLoggedInUser.login)
If myActivity.ID > 0 Then
log.logInDB(clsLoggedInUser.login, "CurrentActivityWindow", "Info", "User already has an activity. Stop it and save or close")
If myActivity.activityStart.HasValue Then
If MessageBox.Show("Already have an active activity, Stop it and create a new one?",
"New Activity", MessageBoxButton.OKCancel, MessageBoxImage.Question) = MessageBoxResult.OK Then
'stop and create new one
myActivity.StopActivity()
myActivity.save()
log.logInDB(clsLoggedInUser.login, "CurrentActivityWindow", "Info", "Setting up New Activity")
newActivity()
Else
Debug.Print("Closing Form - Opened as new activity but user has a current activity and didnt want to stop it.")
Me.Close()
Exit Sub
End If
Else
If MessageBox.Show("Already have an active activity without a start time, will load that up for you.",
"New Activity", MessageBoxButton.OK, MessageBoxImage.Information) = MessageBoxResult.OK Then
End If
End If
Else
log.logInDB(clsLoggedInUser.login, "CurrentActivityWindow", "Info", "No current Activity, calling newActivity")
newActivity()
End If
Case Else
Debug.Print("Error: CurrentActivity Status not set")
log.logInDB(clsLoggedInUser.login, "CurrentActivityWindow", "Info", "Tried opening window without Status being set.")
Application.Current.Shutdown()
End Select
Debug.Print("CurrentActivityWindow - Filling Project List")
buildProjectList()
DataContext = projectsList
Debug.Print("CurrentActivityWindow - Updating GUI")
updateGUI()
末次
感谢您的意见Gabor。我问的问题取决于窗口中的初始化代码。初始化基本上是从数据库中抓取数据,并且在特定情况下,它需要询问用户是否继续。 inisiliasation代码最好留在新窗口而不是调用窗口中,否则我会在调用窗口中看到不相关的代码,最终会导致混乱。 :-)。我最好把所有的初始化代码放在新的构造函数中? – user3844416
您不应该从任何构造函数访问数据库!他们不能包含繁重的任务。考虑使用'OpenSomeWindow()'方法创建'NavigationService'类,该方法可以调用'YourRepository'类中的'GetDataForSomeWindow()'方法,并且根据情况'OpenSomeWindow()'方法可以询问用户并根据答案创建一个NewWindow的新实例,然后显示它。调用者窗口可以有一个'NavigationService'实例并调用它的'OpenSomeWindow()'方法。当然,这些名字只是例子。让我知道你是否需要一个具体的例子实现我的想法 – Gabor
是的。我通常不会将DB代码放在构造函数中,但我对WPF很陌生。 :-)我已经列出了我的代码,希望能够让我更清楚地问清楚。我想在这个窗口中封装逻辑而不是调用窗口,因为调用窗口基本上是一个菜单。你是说这是不可能得到一个消息框,除非我在实际窗口的代码隐藏之外编码它? – user3844416