question

Ivanich avatar image
0 Votes"
Ivanich asked Ivanich commented

UIContext of AppWindow does not match UIContext of it's content

I'm trying to create secondary window using new APIs like in this article: https://docs.microsoft.com/en-us/windows/uwp/design/layout/app-window

In general, it works except that it is impossible to track windows using dictionary like it was shown in that article, because UIContext of created AppWindow does not match UIContext of the Page hosted within that window. Is it some known bug or I do it wrong?

The code:

     public class OpenInNewWindowCommand : DependencyObject, ICommand
     {        
         ...
    
         public async void Execute(object parameter)
         {
             var pageName = Page;
    
             if (!string.IsNullOrEmpty(pageName))
             {
                 var pageType = Assembly.GetEntryAssembly().GetTypes().FirstOrDefault(t => t.IsClass && t.Name == pageName);
    
                 if (pageType != null)
                 {
                     var window = await AppWindow.TryCreateAsync();
    
                     var frame = new Frame();
    
                     ElementCompositionPreview.SetAppWindowContent(window, frame);
    
                     if (RequestedDisplayRegion != null) window.RequestMoveToDisplayRegion(RequestedDisplayRegion);
    
                     if (RequestedSize != Size.Empty) window.RequestSize(RequestedSize);
    
                     if (ExtendViewIntoTitleBar) TitleBarHelper.ExtendView(window);
    
                     var viewsService = Container.Instance.Resolve<IViewsServiceBase>();
    
                     viewsService.RegisterAppWindow(window);
    
                     window.Closed += (s, e) => { frame.Content = null; };
    
                     frame.Navigate(pageType, Parameter);
    
                     await window.TryShowAsync();
                 }
             }
         }
     }

Debugging this on Windows 10 20H2, target and minimal versions are both set to 2004

windows-uwp
5 |1600 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

AryaDing-MSFT avatar image
1 Vote"
AryaDing-MSFT answered Ivanich commented

Hi,

Welcome to Microsoft Q&A!

The reason is that you attached a null XAML content to the created window.

 var window = await AppWindow.TryCreateAsync();    
 var frame = new Frame();
 ElementCompositionPreview.SetAppWindowContent(window, frame); 

As you can see, after you created a frame, you didn’t navigate a page to load content for this frame and use ElementCompositionPreview.SetAppWindowContent() directly.

Therefore, you need to use frame.Navigate(pageType, Parameter) to load page content before you attach the XAML content to the window like the following. So that the UIContext of created AppWindow can match UIContext of the Page hosted within that window.



If the response is helpful, please click "Accept Answer" and upvote it.

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.




· 1
5 |1600 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.

Oh, it's a pity because I need to access window at the step when page is loading to customize title bar. But, well, thank you for the answer!

0 Votes 0 ·