【问题标题】:React to every app activation in Windows Store app对 Windows 应用商店应用中的每个应用激活做出反应
【发布时间】:2012-12-18 10:20:41
【问题描述】:

我有一个使用后台任务进行动态磁贴更新的 Windows 应用商店应用。当我以任何方式激活应用程序时(单击动态磁贴,切换回应用程序等),我想清除动态磁贴(我有一个数字,我想将其更改为零)。

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

我尝试了 App.xaml.cs 中的 OnActivated 方法,但它似乎没有在任何时候被调用(我在那里放置了一个 throw new NotImplementeExeption 并且应用程序永远不会崩溃)。

【问题讨论】:

    标签: windows-8 microsoft-metro


    【解决方案1】:

    你应该把它放在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();
        }
    

    如果您查看代码,您的应用关闭/暂停的原因有多种。因此,确定在哪些情况下您要运行解码代码以更新动态磁贴中的数字,将其放在 if 中,它应该可以工作。

    【讨论】:

      【解决方案2】:

      我想这种操作的更好地方是OnLaunched 方法。每次应用启动时都会调用它。

      更新:嗯,看来你应该对 OnActivatedOnLaunched 方法做出反应:

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

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

      OnActivated - 当应用程序通过正常启动以外的方式激活时调用。

      【讨论】:

      • 你确定吗?我试过了,没有效果。看起来当应用程序正在运行时,我切换到开始屏幕并单击调用注释的应用程序磁贴。这可能是因为如果 Visual Studio。你能推荐什么放在那里来测试它吗?不调试时无法读取Debug.WriteLine
      • 我总是建议放置刹车点并调试以查看它们是否正常工作
      • 不是,我在 OnLaunched 和 OnActivated 中有一个断点,当我从应用程序切换到另一个应用程序,然后再返回时,应用程序不会在断点处停止
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2016-03-31
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多