这是 MainWindow xaml 代码。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="MainWindow"
Height="300"
Width="500">
</Window>
这是 Window1 xaml 代码。
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="Window1"
Height="200"
Width="500">
<Grid>
<DockPanel LastChildFill="False">
<TextBlock DockPanel.Dock="Top" HorizontalAlignment="Stretch" TextWrapping="Wrap" TextAlignment="Justify" Margin="10,20,10,0">
<TextBlock.Inlines>
<Run Name="Inline1" Text="Message"/>
</TextBlock.Inlines>
</TextBlock>
<DockPanel DockPanel.Dock="Bottom" LastChildFill="False">
<Button Name="Button1" Content="No" Width="70" DockPanel.Dock="Right"/>
<Button Name="Button2" Content="Yes" Width="70" DockPanel.Dock="Right"/>
</DockPanel>
</DockPanel>
</Grid>
</Window>
这是 MainWindow vb 代码。
Class MainWindow
Private Sub MainWindow_Closing(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim myWindow As New Window1()
myWindow.Owner = Application.Current.MainWindow
myWindow.Inline1.Text = "Do you really want to quit?"
myWindow.ShowDialog()
e.Cancel = True
End Sub
End Class
这是 Window1 vb 代码。
Public Class Window1
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
System.Windows.Application.Current.Shutdown()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
1-运行这个项目。
2- 单击X以关闭此项目。
3-当项目询问您是否真的要退出时,单击是按钮?
4- 见以下错误;
我想(如果我错了,请纠正我)您想从 [Window1] 接收回输入,这有点像要求关闭的消息框?所以我想有几个按钮 [Yes]/[No] [Confirm]/[Dont Shutdown] 用户点击了?
如果是这样......在Window1类中声明一个公共布尔值,然后在关闭或应用程序关闭之前根据需要将布尔值设置为true / false。
Public Class Window1
Public Confirmed as boolean = false
'I have set to default as boolean as the user could theroretically close the
'form and not click on either button!?
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Confirmed = true
''System.Windows.Application.Current.Shutdown()
me.close
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Confirmed = false
Me.Close()
End Sub
End Class
Class MainWindow
Private Sub MainWindow_Closing(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim myWindow As New Window1()
''myWindow.Owner = Application.Current.MainWindow ''I dont know why you need this?
myWindow.Inline1.Text = "Do you really want to quit?"
myWindow.ShowDialog()
if mywindow.confirmed then
System.Windows.Application.Current.Shutdown()
''or simply..
''application.exit()
else
e.Cancel = True
end if
End Sub
End Class
这绝不是唯一的方法,请不要将此答案视为完全正确,因为您的情况可以通过多种方式实现,其中一种方式是您已经完成的方式。老实说,我可以看到为什么/如何在 showdialog 上发生错误,但是删除窗口并在 MainWindow 中处理会消除 Window1 的问题/错误。
希望这在某种程度上有所帮助 鸡
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句