![](/img/trans.png)
[英]Animate the opening of MainPage from App.xaml.cs in Xamarin Forms
[英]Pass parameter from app.xaml.cs to MainPage
我的应用程序由类似 my-app://path?param1=123"&"param2=abc 的协议启动。
我成功地从 app.xaml.cs 的 OnActivated 处理程序中的查询参数中获取值。
但是我不能覆盖 MainPage 类的 onNavigatedTo 处理程序。 VS 说没有找到 onNavigatedTo。
如何在 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();
}
您可以将导航功能中的任何类型的对象作为参数发送。
我可以在你的代码中看到它,
rootFrame.Navigate(typeof(MainPage), argMessage);
这里"argMessage"
是你想发送的对象,我猜。
如果您想发送任何其他对象,只需传递它而不是argMessage
。
并像这样在您的主页中接收它
var parameters = e.Parameter;
这是一些示例以更好地理解它
希望这可以帮助。
谢谢你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.