wpf里 怎样通过button关闭整个程序?

wpf里 怎样通过button关闭整个程序?,第1张

写了一个例子,你看一下吧。

只要在你的代码中写上 App.Current.Shutdown()即可关闭应侍如孝用程序了。我的机器上只有C#,Vb的用橡配法与C#相同。

只要加一老稿个按钮,在设计器上双击按钮,在事件处理程序中加入上述语句即可。

--------

<Window x:Class="WpfApplication1.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">

<Grid>

<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

</Grid>

</Window>

--------

using System

using System.Collections.Generic

using System.Linq

using System.Text

using System.Windows

using System.Windows.Controls

using System.Windows.Data

using System.Windows.Documents

using System.Windows.Input

using System.Windows.Media

using System.Windows.Media.Imaging

using System.Windows.Navigation

using System.Windows.Shapes

namespace WpfApplication1

{

/// <summary>

/// MainWindow.xaml の相互作用ロジック

/// </summary>

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent()

}

private void button1_Click(object sender, RoutedEventArgs e)

{

App.Current.Shutdown()

}

}

}

--------

WPF关闭当前窗口

public MainWindow()

{

InitializeComponent()

//在属性窗口那里貌似找不到这事件,绝族但是可以手动并空弊注册

this.Closing += F

}

private void F(object o, System.ComponentModel.CancelEventArgs e)

{

if (MessageBox.Show("关闭亏悔", "", MessageBoxButton.YesNo) == MessageBoxResult.No)

e.Cancel = true

}

123456

protected override void OnClosing(CancelEventArgs e){base.OnClosing(e) e.Cancel = true this.Hide() }

加上颂晌岩这段谨枯代码,可以阻止窗口野御关闭,并且隐藏起来。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12561704.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-26
下一篇2025-08-26

发表评论

登录后才能评论

评论列表(0条)

    保存