在 wpf 中显示/关闭表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17011791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing/Closing forms in wpf
提问by danieljohngomez
Im developing an application using wpf template I have these 2 windows: MainWindow.xaml and JungleTimer.vb which is a Windows Form
我使用 wpf 模板开发应用程序我有这两个窗口:MainWindow.xaml 和 JungleTimer.vb,这是一个 Windows 窗体
I have a button in my main windows which shows JungleTimer form using this code:
我的主窗口中有一个按钮,它使用以下代码显示 JungleTimer 表单:
Dim JungleTimer As New JungleTimer
JungleTimer.Show()
But as you see, clicking this button multiple times will show multiple JungleTime form. I tried to use this code to check if JungleTimer is visible but it doesn't work:
但是如您所见,多次单击此按钮将显示多个 JungleTime 表单。我尝试使用此代码来检查 JungleTimer 是否可见,但它不起作用:
Dim JungleTimer As New JungleTimer
If JungleTimer.Visible = False Then
JungleTimer.Show()
End If
I also need the code to close the JungleTimer form.
我还需要关闭 JungleTimer 表单的代码。
回答by BlackWasp
As you are creating a new JungleTimer each time you click the button you will always get a new instance of the window. What you need to do is declare a field within the class of the type JungleTimer. Initially this will be null (Nothing). When you click the button, check if this field has a value or is still null. If still null, set it to a new JungleTimer and show it. If it isn't null, activate the existing window without creating a new instance. you'll also need to detect when the window closes so that you can set the field back to null.
当您每次单击该按钮时都在创建一个新的 JungleTimer 时,您将始终获得该窗口的一个新实例。您需要做的是在 JungleTimer 类型的类中声明一个字段。最初这将为空(无)。单击按钮时,请检查此字段是否有值或仍为空。如果仍然为空,请将其设置为新的 JungleTimer 并显示它。如果它不为空,则激活现有窗口而不创建新实例。您还需要检测窗口何时关闭,以便您可以将该字段设置回 null。
For a demo, create a new WPF application with two windows, MainWindow (the main window) and JungleTimer.
对于演示,创建一个具有两个窗口的新 WPF 应用程序,MainWindow(主窗口)和 JungleTimer。
XAML for MainWindow:
主窗口的 XAML:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel VerticalAlignment="Center">
<Button Width="100" Height="30" Click="Jungle_Click">Jungle Me</Button>
<Button Width="100" Height="30" Click="DeJungle_Click">De-Jungle Me</Button>
</StackPanel>
VB for MainWindow (sorry if it's clumsy, I haven't done VB for ten years or so):
VB for MainWindow(对不起,如果它笨拙,我十年左右没有做VB):
Class MainWindow
Private WithEvents _jungleTimer As JungleTimer
Private Sub Jungle_Click(sender As Object, e As RoutedEventArgs)
If _jungleTimer Is Nothing Then
_jungleTimer = New JungleTimer
_jungleTimer.Show()
Else
_jungleTimer.Activate()
End If
End Sub
Private Sub DeJungle_Click(sender As Object, e As RoutedEventArgs)
If Not _jungleTimer Is Nothing Then
_jungleTimer.Hide()
_jungleTimer = Nothing
End If
End Sub
Private Sub CloseHandler() Handles _jungleTimer.Closed
_jungleTimer = Nothing
End Sub
End Class
XAML for JungleWindow:
JungleWindow 的 XAML:
<Window x:Class="JungleTimer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="JungleTimer" Height="300" Width="300">
<Grid>
<Label HorizontalAlignment="Center" VerticalAlignment="Center">
Jungle!
</Label>
</Grid>

