简体   繁体   English

将参数从 app.xaml.cs 传递到 MainPage

[英]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.

相关问题 在Xamarin Forms中从App.xaml.cs动画打开MainPage - Animate the opening of MainPage from App.xaml.cs in Xamarin Forms 在 UWP 应用程序中,读取命令行 arguments 并将其从 App.xaml.cs 文件传递到 MainPage.Z44CC44B81911F4BA4581C27 - In UWP App, read command line arguments and pass it from App.xaml.cs file to the MainPage.xaml.cs 从Windows Phone 8中的App.Xaml.cs到达MainPage.Xaml.cs单击处理程序函数 - Reaching MainPage.Xaml.cs click handler functions from App.Xaml.cs in windows phone 8 如何从App.xaml.cs调用MainPage.xaml.cs中的函数 - How to call a function that's in MainPage.xaml.cs from App.xaml.cs 如何从 App.xaml 而不是 App.xaml.cs 设置 MainPage? - How to set MainPage from App.xaml instead of App.xaml.cs? 如何在 APP.xaml.cs 代码中进行更改以将参数从一个页面传递到 Xamarin 中的另一个页面? - How to make change in APP.xaml.cs code to pass a parameter from one page to another in Xamarin? 如何在app.xaml.cs中使用具有可变appbar枢轴的MainPage中的方法 - How to use a method in MainPage from app.xaml.cs with variable appbar pivot App.Xaml.cs和MainPage.xaml.cs不能包含在Visual Studio Community 2017的解决方案资源管理器中 - App.Xaml.cs and MainPage.xaml.cs cannot be included in Solutions Explorer of Visual Studio Community 2017 从App.xaml.cs导航 - Navigating from App.xaml.cs 自定义App.xaml.cs - Customizing App.xaml.cs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM