-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