[英]Pass parameter from app.xaml.cs to MainPage
My application is launched by a protocol like my-app://path?param1=123"&"param2=abc .我的应用程序由类似 my-app://path?param1=123"&"param2=abc 的协议启动。
I succeeded to get values from query parameters in app.xaml.cs's OnActivated handler.我成功地从 app.xaml.cs 的 OnActivated 处理程序中的查询参数中获取值。
But I can not override onNavigatedTo handler of MainPage class.但是我不能覆盖 MainPage 类的 onNavigatedTo 处理程序。 VS says onNavigatedTo is not found.
VS 说没有找到 onNavigatedTo。
How can I receive parameters on MainPage?如何在 MainPage 上接收参数?
protected override void OnActivated(IActivatedEventArgs e) {
base.OnActivated(e);
string argMessage = string.Empty;
if (e.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = e as ProtocolActivatedEventArgs;
Debug.WriteLine("Procol URI="+eventArgs.Uri);
argMessage = eventArgs.Uri.Query;
}
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
}
else
{
if (e.Kind == ActivationKind.Protocol)
{
rootFrame.Navigate(typeof(MainPage), argMessage);
}
}
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), argMessage);
}
Window.Current.Activate();
}
You can send any kind of objects in navigation function as param.您可以将导航功能中的任何类型的对象作为参数发送。
I can see it in your code itself,我可以在你的代码中看到它,
rootFrame.Navigate(typeof(MainPage), argMessage);
Here "argMessage"
is a object which you want send i guess.这里
"argMessage"
是你想发送的对象,我猜。
IF you want send any other object , just pass it instead of argMessage
.如果您想发送任何其他对象,只需传递它而不是
argMessage
。
And receive it in your mainpage like this并像这样在您的主页中接收它
var parameters = e.Parameter;
Here is some samples to understand it better这是一些示例以更好地理解它
Navigation Between Pages With Passing Data On Windows 10 在 Windows 10 上通过数据在页面之间导航
Passing complex object between classes in UWP 在 UWP 中的类之间传递复杂对象
Hope this helps.希望这可以帮助。
Thank you.谢谢你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.