Toast 单击时不将参数发送到页面,并且应用程序已打开 C#

本文关键字:应用程序 单击 参数 Toast | 更新日期: 2023-09-27 18:31:31

我有一个接收推送通知的Windows Phone 8.1通用应用程序。单击 Toast 时,它会打开应用并将参数发送到特定页面。我的问题是,仅当我单击 Toast 时应用程序关闭时,它才有效。如果我收到通知时应用处于打开状态,并且我单击了它,它将我定向到该应用,但不会将参数发送到特定页面 (ImageFullScreen)。您知道需要做些什么才能实现这一目标吗?

App.Xaml.Cs:

sealed partial class App : Application
{
    public string NavigateText { get; set; }
    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        var launchString = e.Arguments;
        (App.Current as App).NavigateText = launchString.ToString();
        if (System.Diagnostics.Debugger.IsAttached)
        {
            this.DebugSettings.EnableFrameRateCounter = true;
        }

        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            // Set the default language
            rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
            rootFrame.NavigationFailed += OnNavigationFailed;
            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }
            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }
        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
        // Ensure the current window is active
        Window.Current.Activate();
    }

MainPage.Xaml.Cs:

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        if ((App.Current as App).NavigateText == "")
        {
            await RefreshTodoItems();
        }
        else
        {
            items2 = await todoTable
                .Where(todoItem => todoItem.Text == (App.Current as App).NavigateText)
                .ToCollectionAsync();
            TodoItem myItem = items2[0] as TodoItem;
            Frame.Navigate(typeof(ImageFullScreen), myItem);
        }           
    }

ImageFullScreen.Xaml.Cs:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var myObject = (TodoItem)e.Parameter;
        img.Source = new BitmapImage(new Uri(myObject.ImageUri));
        (App.Current as App).NavigateText = "";
    }

Toast 单击时不将参数发送到页面,并且应用程序已打开 C#

问题是 OnLaunched 方法中的if (rootFrame.Content == null)未执行,因为应用程序已经打开(框架的内容是页面)。仅激活窗口,不调用 MainPage 中的 OnNavigatedTo 方法。

您可以尝试在 OnLaunched 方法中启动导航,始终如下所示(不带 if 语句):

rootFrame.Navigate(typeof(MainPage), e.Arguments);
// Ensure the current window is active
Window.Current.Activate();