wpf 关闭界面
时间: 2023-07-16 10:14:44 浏览: 54
在 WPF 中,关闭界面可以使用以下几种方法:
1. 在代码中调用窗口的 Close() 方法:
```csharp
private void closeButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
```
2. 在 XAML 中使用按钮的 Click 事件:
```xaml
<Button Content="Close" Click="closeButton_Click" />
```
```csharp
private void closeButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
```
3. 在窗口的 XAML 中添加一个关闭按钮:
```xaml
<Window ...
Title="My Window">
<Grid>
<!-- Window contents here -->
</Grid>
<DockPanel LastChildFill="False" HorizontalAlignment="Right" Margin="5">
<Button Name="closeButton" Content="Close" Click="closeButton_Click"/>
</DockPanel>
</Window>
```
```csharp
private void closeButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
```
无论哪种方法,都是调用窗口的 Close() 方法来关闭窗口。