WinUi3: How to open WinUI's window like wpf's ShowDialog

Viewed 206
MainWindow window = new MainWindow();
window.Activate();

This is the function provided by Microsoft to open a new window. doc link

But using Activate() is not the effect I want, it is hidden behind the parent window, and it seems that there is no interface to adjust the size and position of the widnow.

I tried my best, but I still can't find a way.Below are some attempts.

LgnRegWindow lgnRegWin = new LgnRegWindow();
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(lgnRegWin);
WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
AppWindow appWindow = AppWindow.GetFromWindowId(myWndId);
OverlappedPresenter _presenter = appWindow.Presenter as OverlappedPresenter;

appWindow.Resize(new SizeInt32(600, 800));
_presenter.IsResizable = false;
appWindow.Show();

This cannot be done like ShowDialog().

1 Answers

If you need modal dialog, you should better use ContentDialog

Adjust its content in XAML and then call it where necessary:

ContentDialogResult result = await contentDialog.ShowAsync();
// check result it returns if necessary
Related