英文:
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论