如何关闭wpf应用程序时运行的函数? [英] How can I write a function that runs when closing a wpf application?

查看:182
本文介绍了如何关闭wpf应用程序时运行的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个在关闭wpf应用程序时运行的函数.我该如何为Windows关闭按钮(位于窗口的右上角)执行此操作?

I want to write a function that runs on closing a wpf application. How can I do this for the Windows close button (which is there in the top right corner of a window)?

推荐答案

您将在获取事件处理程序时自动获得该事件处理程序.将从XAML页面的窗口"标记中的帮助列表中选择关闭"事件.当您通过按"closing =""逗号内的ctrl +空格键选择新EventHandler"时,它将自动在后面的代码处生成事件.

XAML文件示例:

You will get the event handler automatically when you will select the Closing event from the help list in the "window" tag at XAML page. It will automatically generate an event at the code behind when you will select "new EventHandler" by pressing ctrl+spacebar inside the ''closing=""'' commas.

XAML file example:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Closing="Window_Closing" Closed="Window_Closed" >



示例后面的代码:



Code behind example:

private void Window_Closed(object sender, EventArgs e)
{
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e
{
}



希望这对您有所帮助:rose:

Anurag



Hope this helps :rose:

Anurag


...可能我最初没有正确地阅读问题,但由于某种原因而被否决.答案是:

…probably I did not read the question properly in first place, down-voted for a reason. Here is the answer:

public partial class MyWindow : Window {
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
        base.OnClosing(e);
        // the process of closing can be canceled;
        // uncomment two next lines for a typical use of it:
        //e.Cancel = true;
        //Hide();
    }


    protected override void OnClosed(EventArgs e) {
        base.OnClosed(e);
        //...
    }



或者,这是基于事件的方法(由于窗口子类仍是子类,因此实际上并不需要此方法:



Alternatively, this is the event-based approach (which is not really needed because Window is sub-classed anyway:

MyWindow.Closed += (sender, eventArgs) => {
    //...
};
MyWindow.Closing += (sender, eventArgs) => {
    //..
    // the process of closing can be canceled;
    // uncomment two next lines for a typical use of it:
    //e.Cancel = true;
    //Hide();
};



两种方法都比设计器自动生成的代码好得多.有趣的是,仍然使用了不基于匿名方法的代码.它的支持较少,但可能更易于生成(或代码生成使用可解释的旧代码).基于匿名的方式更短,并且有助于支持,因为不需要编写具有精确签名的方法和编写类型名称.另外,以lambda形式从事件类型中推断出参数的类型.

—SA



Both ways are much better than the code auto-generated by the Designer. It''s funny that the code not based on anonymous methods is still used. It is less supportable but probably easier of generation (or code generation uses legacy code, which is explainable). Anonymous-based way is shorter and helps supportability, because writing a method with exact signature and writing type names is not required. Additionally, in lambda form the types of arguments are inferred from event type.

—SA


在您的时间里非常感谢,但是我需要编写一些代码来关闭窗口功能,我知道如何关闭应用程序. /strike>

对于OP:除非要提供解决问题的方法,否则应该通过改善问题"或添加评论"来提供信息. :)

您的评论已移至预期收件人的评论部分. :cool:
Thnaks so much for your time But I need to write some code for close window functionality, i know how to close the application.

For OP: You are supposed to provide information by either "improving your question" or by "adding a comment" to your question, unless you are providing a solution to your problem. :)

You comment has been moved to the comments section of intended recipient. :cool:


这篇关于如何关闭wpf应用程序时运行的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆