Nếu bạn muốn làm điều đó tất cả các từ mã sau đặt này trong cửa sổ của bạn cs tập tin
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Closed += new EventHandler(MainWindow_Closed);
}
void MainWindow_Closed(object sender, EventArgs e)
{
//Put your close code here
}
}
}
Nếu bạn muốn tham gia vào xaml và một phần mã sau của mình trong XAML
<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" Closed="MainWindow_Closed">
<Grid>
</Grid>
</Window>
và điều này trong cs
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void MainWindow_Closed(object sender, EventArgs e)
{
//Put your close code here
}
}
}
ở trên để ví dụ bạn có thể áp dụng cho dưới mọi hình thức trong một ứng dụng XAML. Bạn có thể có nhiều biểu mẫu. Nếu bạn muốn áp dụng mã cho toàn bộ quá trình thoát ứng dụng, hãy sửa đổi tệp app.xaml.cs của bạn thành
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnExit(ExitEventArgs e)
{
try
{
//Put your special code here
}
finally
{
base.OnExit(e);
}
}
}
}
Nguồn
2012-04-04 20:02:17
Xem tại đây và sử dụng 'OnExit' thay vì' OnStartup'. http://manaspatnaik.com/blog/index.php/technology/wpf/event-handling-in-wpf/193 – mydogisbox