2016-05-12 70 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