处理程序,以查看Page是否在被暂停之前启动

时间:2014-11-01 12:34:46

标签: c# windows-phone-8.1 win-universal-app

我正在构建一个带有SecondaryTile的应用,以使用一些参数启动它。

我需要一种方法来了解MainPage是否已从SecondaryTile启动,因此我尝试使用{{1}来查看是否有Argument OnNavigatedTo方法。

问题是,只有当应用程序从挂起状态启动时才会触发此事件,这意味着如果按下Windows按钮然后在10秒之前点击磁贴,则不会触发该事件。

这同样适用于Loaded事件和NavigationHelper_LoadState hanlder,因此我不知道如何让页面知道它是从SecondaryTile启动的。< / p>

我知道我可以从App OnLaunched事件中获取此信息,但我仍然不知道如何将其传递给MainPage,因为似乎没有方法在页面显示后调用。

即使sample provided by Microsoft失败了,因为它使用构造函数和OnNavigatedTo处理程序,而且我不知道如何使其工作。

2 个答案:

答案 0 :(得分:1)

将激活参数存储在App类...

internal static LaunchActivatedEventArgs LaunchArgs = null;
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
  LaunchArgs = e;

  // rest of method

...并从目标网页的OnNavigatedTo方法中读取它们(您链接到的示例中的“方案5”):

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  if (App.LaunchArgs != null && !String.IsNullOrEmpty(App.LaunchArgs.Arguments))
    InputText.Text += Environment.NewLine + "Launched from secondary tile" 
      + Environment.NewLine + DateTime.Now.ToString("hh:mm:ss");
  else
    InputText.Text += Environment.NewLine + "No launch info available" 
      + Environment.NewLine + DateTime.Now.ToString("hh:mm:ss");

  // rest of method

请注意,如果您支持其他激活类型(文件,协议等),则在这些情况下您需要清空LaunchArgs字段。

答案 1 :(得分:0)

由于OnNavigatedTo在从之前的Running状态启动应用时未被触发,因此我必须使用此OnLaunched方法APp.xaml.cs方法强制导航{1}}:

if (rootFrame.Content == null)
{
    // Default generated code
}
else
{
    if (e.PreviousExecutionState == ApplicationExecutionState.Running)
    {
        if (!string.IsNullOrEmpty(e.Arguments))
            if (!rootFrame.Navigate(typeof (MainPage), e.Arguments))
            {
                 throw new Exception("Failed to create initial page");
            }
    }
}

// Ensure the current window is active.
Window.Current.Activate();

这允许触发OnNavigatedTo方法,因此我可以管理从辅助磁贴启动应用的情况。

(我不知道粘贴的代码是否有效,因为我必须在粘贴之前更改它,但这是个想法)