UWP on desktop closed by top X button - no event

后端 未结 4 1631
温柔的废话
温柔的废话 2020-12-01 18:06

An UWP app which runs on desktop can be closed from the top X button but it doesn\'t have any event for it. It is known that on phones and tablets an app should rely on

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 18:48

    A restricted capability confirmAppClose was added in Windows 10 version 1703 (build 10.0.15063) in order to provide apps the ability to intercept window closing.

    Manifest namespace:

    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    

    Manifest:

     
       
       
     
    

    It needs extra approval when submitting to the store. But then will fire the CloseRequested event on a SystemNavigationManagerPreview instance.

    Code:

        public MainPage()
        {
            this.InitializeComponent();
            SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += this.OnCloseRequest;
        }
    
        private void OnCloseRequest(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
        {
            if (!saved) { e.Handled = true; SomePromptFunction(); }
        }
    

    You can get a deferral to do a bit of work here (save or prompt), or you can set Handled to true in order to stop the window from closing (user cancelled prompt).

提交回复
热议问题