Search code examples
winui-3c++-winrtwindows-app-sdk

How to pass parameters to the constructor through Frame.Navigate in C++/WinRT?


I just tried to use box_value, but it seems that the non-parameterized constructor will be always called. After I deleted the default constructor, XamlUserType::ActivateInstance() just crashed with null pointer exception.

How to navigate to Parametrised Constructor in UWP?

I found a C# version of solution to this problem, but I'm not sure what is the equivalent way in C++/WinRT.


Solution

  • As Raymond Chen said, you need to get your parameters in OnNavigatedTo. You can refer to the following code.

    //ConfigPage.xaml.h
    
    void OnNavigatedTo(Microsoft::UI::Xaml::Navigation::NavigationEventArgs const&);
     
    //ConfigPage.xaml.cpp
    
    void ConfigPage::OnNavigatedTo(Microsoft::UI::Xaml::Navigation::NavigationEventArgs const& e)
    {
       auto window = unbox_value<Microsoft::UI::Xaml::Window>(e.Parameter());     
    }