在某些条件下阻止应用程序关闭 [英] Prevent Application From Shutdown on Certain Condition

查看:90
本文介绍了在某些条件下阻止应用程序关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何阻止应用程序从基于xaml关闭?我有Application.xaml,它有Me.Startup和Me.Exit来处理对象初始化和处理。如果我的程序中的任何页面靠近Alt + F4,我会在某些情况下检查Me.Exit。如果为True,则继续处理对象并退出。但是当False时,它应取消退出并留在最后一页。



如何做到这一点?我使用Page.xaml而不是Window.xaml创建应用程序界面,并且从我搜索的内容中创建应用程序界面,大部分都显示在Windows_Closing上。我需要的是取消False,就像Winforms一样,可以使用e.Cancel = True但在WPF中。



请帮忙。谢谢



更新

:使用PAGE.XAML运行界面而不是WINDOW.XAML

Hi all,

How to preventing application to shutdown from based xaml? I have Application.xaml which have Me.Startup and Me.Exit to handle object initialization and dispose. If any page on my program is close by Alt+F4, I will check at Me.Exit with certain condition. If True, the it continue dispose object and exit. But when False, it should cancel the exit and stay on last page.

How to do this? I create application interface using Page.xaml not Window.xaml and from what I search, most of it show on Windows_Closing.. What I need is cancelling when False just like Winforms which can use e.Cancel = True but in WPF.

Kindly help. Thanks

UPDATED
: RUNNING INTERFACE USING PAGE.XAML NOT WINDOW.XAML

推荐答案

你可以这样做:

You can do like this:
<window x:class="WpfApplication7.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Closing="Window_Closing">
    <grid>
        
    </grid>
</window>







using System.Windows;

namespace WpfApplication7
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}





您必须通过设置处理Closing事件e.Cancel = true。

当然你必须在设置e.Cancel = true之前检查你的情况!



You must handle the Closing event by setting e.Cancel = true.
Of course you must check your condition before setting e.Cancel = true!


这篇关于在某些条件下阻止应用程序关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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