当WPF窗口的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:如何在不将WPF窗口显示为背后的黑框的情况下获取messageBox
这是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()
End Sub
答案 0 :(得分: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
的新实例之前,在调用者窗口的按钮点击事件处理程序中提问是一个更好的解决方案。