0

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.

Ax1le
  • 6,563
  • 2
  • 14
  • 61
ACGMN
  • 9
  • 3
  • Is it UWP or WinUI3? They are *not* the same technology, and you should show your C# solution, it maybe translatable. – Simon Mourier May 22 '23 at 16:09
  • It's a WinUI 3 application. I passed the parameter [here](https://github.com/ACGNnsj/WinUI3CppWinRTDemo/blob/61eb2a6a062370808a8768bbadd297b0deed3bc2/OCR/NavigationPage.xaml.cpp#LL61C9-L61C9), and deleted the default constructor [here](https://github.com/ACGNnsj/WinUI3CppWinRTDemo/blob/61eb2a6a062370808a8768bbadd297b0deed3bc2/OCR/ConfigPage.idl#L10). – ACGMN May 23 '23 at 00:43
  • As the linked answer calls out, the parameter arrives not through the constructor but through the OnNavigatingTo and OnNavigatedTo arguments. – Raymond Chen May 23 '23 at 02:56
  • Thanks a lot. I've taken that as some kind of custom function, and thus didn't think much about it. – ACGMN May 23 '23 at 06:11

1 Answers1

0

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());     
}
Junjie Zhu - MSFT
  • 2,086
  • 1
  • 2
  • 6
  • 1
    Yeah, and I find that Window itself inherits from IInspectable, so it doesn't need boxing. – ACGMN May 23 '23 at 06:30