2016-05-12 40 views
-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() 

末次

回答

1

你真的需要加載彈出WPF窗口你問你的問題之前?

回答您的Question1: 您可以在彈出的WPF窗口的構造函數中調用MessageBox.Show()

我會做這樣的事情:

第1步:假設有你的情況下,PopupWindow

Public Class PopupWindow 

    ' We use this custom property to decide whether we need to show this window or not. 
    Public Property CanOpen As Boolean 

    Public Sub New() 
     ' This call is required by the designer. 
     InitializeComponent() 
     ' Add any initialization after the InitializeComponent() call. 
     CanOpen = True 
     ' To not display the WPF window you can ask your question from the constructor 
     If MessageBox.Show(
      "Question?", "Ask", MessageBoxButton.OKCancel, MessageBoxImage.Question 
     ) = MessageBoxResult.Cancel Then 
      CanOpen = False 
     End If 
    End Sub 
End Class 

步驟#2:在呼叫者窗口例如在按鈕點擊事件處理程序中,我們決定是否打開PopupWindow

Private Sub Button_Click(sender As Object, e As RoutedEventArgs) 
    Dim popupWindow As PopupWindow = New PopupWindow() 

    ' At this point our custom property already contains the value 
    ' based on the answer provided by the user to our question 
    ' because the question is asked within the constructor 
    ' we called in the previous statement. 
    If (Not popupWindow.CanOpen) Then 
     ' Do what you would do in PopupWindow.Closed event 
     Exit Sub 
    End If 

    ' Until this point the window is not shown. 
    popupWindow.Owner = Me 
    popupWindow.ShowDialog() 

    MessageBox.Show("Popup closed.", "Info", MessageBoxButton.OK, MessageBoxImage.Information) 
End Sub 

請注意:這是一個更好的解決方案創建PopupWindow的新實例之前,請於呼叫窗口的按鈕單擊事件處理你的問題。

+0

感謝您的意見Gabor。我問的問題取決於窗口中的初始化代碼。初始化基本上是從數據庫中抓取數據,並且在特定情況下,它需要詢問用戶是否繼續。 inisiliasation代碼最好留在新窗口而不是調用窗口中,否則我會在調用窗口中看到不相關的代碼,最終會導致混亂。 :-)。我最好把所有的初始化代碼放在新的構造函數中? – user3844416

+0

您不應該從任何構造函數訪問數據庫!他們不能包含繁重的任務。考慮使用'OpenSomeWindow()'方法創建'NavigationService'類,該方法可以調用'YourRepository'類中的'GetDataForSomeWindow()'方法,並且根據情況'OpenSomeWindow()'方法可以詢問用戶並根據答案創建一個NewWindow的新實例,然後顯示它。調用者窗口可以有一個'NavigationService'實例並調用它的'OpenSomeWindow()'方法。當然,這些名字只是例子。讓我知道你是否需要一個具體的例子實現我的想法 – Gabor

+0

是的。我通常不會將DB代碼放在構造函數中,但我對WPF很陌生。 :-)我已經列出了我的代碼,希望能夠讓我更清楚地問清楚。我想在這個窗口中封裝邏輯而不是調用窗口,因爲調用窗口基本上是一個菜單。你是說這是不可能得到一個消息框,除非我在實際窗口的代碼隱藏之外編碼它? – user3844416