如何通过C++/WinRT中的Frame.Navigate传递参数给构造函数?

huangapple go评论62阅读模式
英文:

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

问题

我刚刚尝试使用box_value,但似乎总是调用非参数化构造函数。在删除默认构造函数后,XamlUserType::ActivateInstance()仅会导致空指针异常崩溃。

我找到了这个问题的C#版本解决方案,但我不确定在C++/WinRT中的等效方式是什么。

https://stackoverflow.com/questions/35649837/how-to-navigate-to-parametrised-constructor-in-uwp

英文:

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.

https://stackoverflow.com/questions/35649837/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.

答案1

得分: 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_valueMicrosoft::UI::Xaml::Window(e.Parameter());
}

英文:

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

huangapple
  • 本文由 发表于 2023年5月22日 23:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定