Handling of UWP child window closing behavior

DotNET Fan 191 Reputation points
2024-07-05T14:15:02.29+00:00

Hello UWP experts ,

I create the child windows from the main app using the below code.

The requirement is at the start dont allow the user to close of window and allow only after 10 to 20 seconds. I couldn't find any pre close event for the child windows . Is there a way to handle this?

CoreApplicationView newView = CoreApplication.CreateNewView();
  int newViewId = 0;
  await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
  {
      Frame frame = new Frame();
      frame.Navigate(typeof(XXXXXPage));
      ApplicationView.PreferredLaunchViewSize = new Size(480, 800);
      ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
      Window.Current.Content = frame;
      Window.Current.Activate();
      newViewId = ApplicationView.GetForCurrentView().Id;
  });
  bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);

Closer to this requirement is a content dialog with custom buttons but it is having sticky behavior and cant move the window . It doesn't come with a minimize/Maximize options.

Please let me know your views on this.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 32,726 Reputation points Microsoft Vendor
    2024-07-08T03:22:37.4+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Handling of UWP child window closing behavior

    If I understand you correctly, user could launch an extra View in your app and you want to make sure that the extra View can't be closed in 10-20s at the beginning. Is that right?

    A possible solution is that you could handle the SystemNavigationManagerPreview.CloseRequested in the extra View (Let's say it is called BlankPage1) to make sure user can't close at the beginning. Then you could start a timer and use a flag to indicate when it is allowed to close the extra View.

    Besides, it is recommended to tell the user when user close the view.

    Here is the code that you could refer to. You could create such a page and test with your code.

       public sealed partial class BlankPage1 : Page
       {
           public bool allowClose = false;
           public DispatcherTimer timer;
           public BlankPage1()
           {
               this.InitializeComponent();
               // launch a timer
               timer = new DispatcherTimer();
               timer.Tick += Timer_Tick;
               timer.Interval = new TimeSpan(0, 0, 5);
               timer.Start();
               // handle close event
               var sysNavMgr = SystemNavigationManagerPreview.GetForCurrentView();
               sysNavMgr.CloseRequested += SysNavMgr_CloseRequested;
           }
    
           private void Timer_Tick(object sender, object e)
           {
               Debug.Write("5s passed");
               allowClose = true;
               timer.Stop();
           }
    
           private void SysNavMgr_CloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
           {
               var deferral = e.GetDeferral();
    
               if (!allowClose) 
               {
                   e.Handled = true;
               }
               
               deferral.Complete();
           }
       }
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments