Positioning UWP ContentDialog on top of AppWindow.

DotNET Fan 271 Reputation points
2025-06-20T03:31:24.36+00:00

In UWP app , I have a AppWindow which is opened from the main window with the below code.

var appWindow = await AppWindow.TryCreateAsync();
var frame = new Frame();
frame.Navigate(typeof(AppWindowPage));
ElementCompositionPreview.SetAppWindowContent(appWindow, frame);
appWindow.Closed += AppWindow_Closed;
appWindow.CloseRequested += AppWindow_CloseRequested;
appWindow.TitleBar.ExtendsContentIntoTitleBar = true;
appWindow.TitleBar.ButtonBackgroundColor = Color.FromArgb(0, 26, 26, 26);
appWindow.TitleBar.ButtonForegroundColor = Colors.White;
appWindow.TitleBar.ButtonInactiveBackgroundColor = Color.FromArgb(0, 26, 26, 26);
await appWindow.TryShowAsync();

When a contentdialog is opened from the appwindow , it goes behind the appwindow and stays as modal to the main window instead of AppWindow. I have set the XamlRoot of content dialog to the XamlRoot AppWindowPage XamlRoot, but still it doesn't work.


var dialog = new ContentDialog
    {
        Title = "AppWindow Dialog",
        Content = "This appears in AppWindow.",
        PrimaryButtonText = "OK",
        CloseButtonText = "Cancel",
        XamlRoot = ViewsContext.XamlRoot//AppWindowPage XamlRoot.

    };
    await dialog.ShowAsync();

How to make it work so that the owner is AppWindow and stick to the Appwindow from where the dialog is invoked?

Developer technologies | Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Harry Vo (WICLOUD CORPORATION) 320 Reputation points Microsoft External Staff
    2025-06-30T03:04:10.0333333+00:00

    It looks like the ContentDialog is defined outside the context of the AppWindowPage itself. Ideally, make sure that ContentDialog is defined within the code-behind AppWindowPage.xaml.cs, so it remains tied to the correct visual tree and dispatcher thread of the AppWindow.

    Also, you might want to update this line inside of ContentDialog:

    var dialog = new ContentDialog 
    	{ 
    		...
    		XamlRoot = ViewsContext.XamlRoot//AppWindowPage XamlRoot. 
    		...
    	}; 
    	await dialog.ShowAsync();
    

    to:

    var dialog = new ContentDialog 
    	{ 
    		...
    		XamlRoot = this.XamlRoot
    		...
    	}; 
    	await dialog.ShowAsync();
    

    This ensures that the dialog truly uses the XamlRoot of the current page instance rather than relying on a static reference.

    In addition, the OnNavigated function in AppWindowPage may be contributing to the issue. If ViewsContext.XamlRoot is stale, delayed, or assigned too late, the ContentDialog might not correctly anchor to the intended window’s visual tree. You can try to remove ViewsContext.XamlRoot = this.XamlRoot; in OnNavigated function to check that if it works.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.