对Windows应用商店应用中的每个应用激活做出反应

时间:2012-12-18 10:20:42

标签: windows-8 microsoft-metro

我有一个使用后台任务进行Live Tile更新的Windows应用商店应用。当我以任何方式激活应用程序时(点击实时图块,切换回应用程序等等。)我想清除实时图块(我有一个数字,我想要更改为零)。

为了更加安静,我运行应用程序,我切换到另一个应用程序或桌面,然后切换到星形屏幕,我在Live Tile上看到一个数字。我点击Live Tile,我被带到应用程序,我希望Live Tile清除。与电子邮件应用程序相同的功能。

我在App.xaml.cs中尝试过OnActivated方法但是它似乎随时都没有被调用(我在那里抛出了一个新的NotImplementeExeption而且应用程序永远不会崩溃)。

2 个答案:

答案 0 :(得分:0)

我想这种行为的最佳位置是OnLaunched方法。每当你开始申请时都会打电话。

更新:嗯,您似乎应对OnActivatedOnLaunched方法做出反应:

  

OnLaunched - 启动应用程序时调用。覆盖这个   执行应用程序初始化和显示初始化的方法   相关窗口中的内容。

在应用程序启动OnLaunched将被调用。但是,当您切换到另一个应用程序然后返回OnActivated应该被称为:

  

OnActivated - 通过除正常启动之外的某些方式激活应用程序时调用。

答案 1 :(得分:0)

你应该把它放在OnLaunched方法中,你只需要确定在哪里。

 protected async override void OnLaunched(LaunchActivatedEventArgs args)
    {
        var rootFrame = new Frame();
        // Do not repeat app initialization when already running, just ensure that
        // the window is active
        if (args.PreviousExecutionState == ApplicationExecutionState.Running)
        {
           //....
        }

        if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
        {
            /....
        }
        if (!String.IsNullOrEmpty(args.Arguments))
        {
                //....

        }
        if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //....
        }
        if (args.PreviousExecutionState == ApplicationExecutionState.NotRunning)
        {
            //.....
        }
        TileUpdateManager.CreateTileUpdaterForApplication().Clear();
        BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();

        SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
        // Create a Frame to act navigation context and navigate to the first page

        if (!rootFrame.Navigate(typeof(MainPage)))
        {
            throw new Exception("Failed to create initial page");
        }

        // Place the frame in the current Window and ensure that it is active
        Window.Current.Content = rootFrame;
        Window.Current.Activate();
    }

如果您查看代码,有几个原因可以解释您的App被关闭/暂停的原因。因此,确定您要在哪些情况下运行de代码以更新Live Tile中的数字,将其放在if中,并且它应该有效。

相关问题