question

Madhu-8808 avatar image
0 Votes"
Madhu-8808 asked ·

UWP page close

Hi,

In our UWP app, there are certain pages which we need to get working like a Modal. Mainly when it comes to closing it, we need to maintain the status of the previous page as it is without reloading it.
We load the next page like this rootFrame.Navigate(typeof(HelpScreen), e.Arguments); Is there a way we can close this and go to the previous page without reloading the previous page. It has to be in the state where it was before navigating to this Page. Is it possible? If so please let me know how to do that.


If that is not possible I think the other option I have is to use AppWindow. I tried that with the samples here.

https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/AppWindow

One issue for our app with that is it creates a separate window, so there are 2 windows in the app. But it'll be good if we can do it without creating another window.
Anyway if this is the only option and if I go with that there is another issue. Our app is running in FullScreen.

ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;

When it is in full screen mode and when I close the second window, the main window is minimized. Can you please tell me how to have it opened like in non full screen mode.

UPDATE : Our client doesn't like opening another window. He wants to have the second screen also opening in the same windows. So is there an option to close a page which we navigates using rootFrame.Navigate(typeof(HelpScreen), e.Arguments); without refreshing the previous page on close? I don't think we can use a content dialog. This is a full page with grids and other components inside it.

Thank you very much.
Madhurya

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
1 Vote"
NicoZhu-MSFT answered ·

In our UWP app, there are certain pages which we need to get working like a Modal.

Derive from official document ,

Dialogs are modal UI overlays that provide contextual app information. Dialogs block interactions with the app window until being explicitly dismissed. They often request some kind of action from the user.

For creating dialog control please refer this tutorial . For example:

 private async void DisplayNoWifiDialog()
 {
     ContentDialog noWifiDialog = new ContentDialog
     {
         Title = "No wifi connection",
         Content = "Check your connection and try again.",
         CloseButtonText = "Ok"
     };
    
     ContentDialogResult result = await noWifiDialog.ShowAsync();
 }

And you could also use Popup control to implement modal page effect. The following is PopupControl that use to show a modal page.



 public sealed partial class PopupControl : UserControl
 {
     Popup popup;
     public PopupControl()
     {
         this.InitializeComponent();
         SystemNavigationManager.GetForCurrentView().BackRequested += ModalPage_BackRequested;
         Window.Current.CoreWindow.SizeChanged += CoreWindow_SizeChanged;
     }
    
    
     private void CoreWindow_SizeChanged(CoreWindow sender, WindowSizeChangedEventArgs args)
     {
         UpdateUI();
     }
    
     private void ModalPage_BackRequested(object sender, BackRequestedEventArgs e)
     {
         Close();
    
     }
     private void UpdateUI()
     {
    
         var bounds = Window.Current.Bounds;
         this.Width = bounds.Width;
         this.Height = bounds.Height;
     }
    
     public void Show()
     {
         popup = new Popup();
         popup.Child = this;
        
         popup.IsOpen = true;
         UpdateUI();
     }
    
    
    
     public void Close()
     {
         if (popup.IsOpen)
         {
             
             popup.IsOpen = false;
             SystemNavigationManager.GetForCurrentView().BackRequested -= ModalPage_BackRequested;
             Window.Current.CoreWindow.SizeChanged -= CoreWindow_SizeChanged;
         }
     }
    
     private void ClosePopupClicked(object sender, RoutedEventArgs e)
     {
         Close();
     }
 }

Xaml

alt text

Usage

  PopupControl p = new PopupControl();
  p.Show();



· 8 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much @NicoZhuShanghaiWicresoftCoLtd-8876 I think going with Popup control is the solution for my issue. One question on that. Now I have to change my page as usercontrol in order to get this working. When I do that, if I want to navigate to another page/user control, how can I do that. I have seen this as a solution in several places, but that doesn't seem to work ((Frame)Window.Current.Content).Navigate(typeof(NextScreen));

0 Votes 0 ·

The popup page does not insert to navigation stack. if you want to navigate to other page from popup UserControl, you need add the Frame into popup UserControl and navigate with it, and please note you need pass the page type to Navigate method, in other word, you could not pass other popup user control to Navigate mehod.

0 Votes 0 ·

@NicoZhuShanghaiWicresoftCoLtd-8876 Now I have implemented the code to open the UI as a UserControl popup as in the example you provided. This UserControl get opened from a Page. Now when I close this popup I need to call a function in the previous page (the page where this pop up was opened). Can you please guide me how we can call a method in the earlier page when closing this popup. Thank you very much for all the help.

0 Votes 0 ·
Show more comments