You can use the Window class, with EnableWindow to disable/enable parent windows :
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
PrivacyWindow::PrivacyWindow() {
InitializeComponent();
//......
window_handle_ = GetWindowHandle();
auto window_id = winrt::Microsoft::UI::GetWindowIdFromWindow(window_handle_);
app_window_ = winrt::Microsoft::UI::Windowing::AppWindow::GetFromWindowId(window_id);
auto presenter = app_window_.Presenter().as<winrt::Microsoft::UI::Windowing::OverlappedPresenter>();
presenter.IsResizable(false);
presenter.IsMaximizable(false);
presenter.IsMinimizable(false);
presenter.IsModal(true); // crash here
app_window_.SetPresenter(presenter);
//......
}
I tried IsModal of OverlappedPresenter, but it crashes.
I think the reason is that the owner of the window is not set to be MainWindow. But I don't know how to set it up.
ps:I'm sure what I want to pop up is a modal window and not a dialog。
You can use the Window class, with EnableWindow to disable/enable parent windows :
For anyone else coming here looking for a straightforward answer, here it is. In order to create a modal window, you have to create the window manually using the AppWindow
class. The Window
class can't be used to create it.
When creating the AppWindow
, pass it a newly constructed OverlappedPresenter
that has IsModal=true
. And here's the most important part: when creating the AppWindow
, you must use the overloaded version of the Create()
method that takes in the ID of an owner window. Set the ID to the ID of the window that the modal window will be associated with. If you don't do this, you will get the following error which explains absolutely nothing about what the problem is:
System.ArgumentException: 'Value does not fall within the expected range.'
If you're creating a modal window, make it non-resizable if you can. Otherwise, you have to deal with the strange behavior that comes from the user minimizing it.
Full working solution:
// Proper option for modal windows in most cases is "Dialog" (non-resizable).
// "this" is some other Window.
{
var presenter = OverlappedPresenter.CreateForDialog();
presenter.IsModal = true;
var appWindow = AppWindow.Create(presenter, this.AppWindow.Id);
appWindow.Show();
}
Why should I design a UI with XAML if its not shown?