簡體   English   中英

將參數從 app.xaml.cs 傳遞到 MainPage

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

這是一些示例以更好地理解它

在 Windows 10 上通過數據在頁面之間導航

在 UWP 中的類之間傳遞復雜對象

希望這可以幫助。

謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM