Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 23-Dec-14 17:55pm
v2

1 solution

You can do like this:
C#
<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>


C#
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;
        }
    }
}


You must handle the Closing event by setting e.Cancel = true.
Of course you must check your condition before setting e.Cancel = true!
 
Share this answer
 
v2
Comments
Luiey Ichigo 18-Dec-14 22:50pm    
But...I don't use Window. I use Page. This application is full screen without any border or titlebar. So, Page offer Page_Unloaded only
Hung, Han 1-Jan-15 21:44pm    
Just to clarify, are we talking about a Windows application? Or a browser hosted control/page? Based on your last comment, it sounds like a browser hosted control.
Luiey Ichigo 2-Jan-15 3:36am    
In add new item, there are Window (WPF). Page (WPF) and the other things. I use page. Not window. And in my page, i put a browser control. My page xaml starts with (Page x:Class=Page1) where Window xaml start with (window x:class=Window1). the one that you show is for using window.i use page. page not have windows closing as my application set kiosk evironment(full screen). apologies if are clear with question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900