[Winui3]How to show a model window?

lei han 46 Reputation points
2022-07-01T08:22:42.937+00:00
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。

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
693 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 80,891 Reputation points
    2022-07-01T08:47:02.093+00:00

    You can use the Window class, with EnableWindow to disable/enable parent windows :

    216893-modal-windows.gif

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. 徐凤辉 1 Reputation point
    2023-03-31T08:18:32.1133333+00:00
    0 comments No comments

  2. eFail 0 Reputation points
    2023-07-27T05:41:53.7633333+00:00

    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();
    }