简体   繁体   中英

Pass parameter from app.xaml.cs to MainPage

My application is launched by a protocol like my-app://path?param1=123"&"param2=abc .

I succeeded to get values from query parameters in app.xaml.cs's OnActivated handler.

But I can not override onNavigatedTo handler of MainPage class. VS says onNavigatedTo is not found.

How can I receive parameters on 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.

IF you want send any other object , just pass it instead of 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

Passing complex object between classes in UWP

Hope this helps.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM