Windows Phone 8.1 后台代理分别调用自定义方法

Windows Phone 8.1 Background Agent invokes a custom method severally

我的 windows phone 8.1(C#) 有一个后台代理,用于监听推送通知。 我还有一个前台应用程序,如果它是 运行,它会处理推送通知。 问题是后台代理多次调用自定义方法 await SyncPushChanges.initUpdate(true); 导致我的 sqlite 数据库中出现重复值。 这是前台委托的代码:

static async void channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
        {  
           //Init update from the server
           await SyncPushChanges.initUpdate();
           //Prevent background agentt from being invoked 
           args.Cancel = true;

        }

Code In background Agent 



  public async void Run(IBackgroundTaskInstance taskInstance)
            {
                var deferal = taskInstance.GetDeferral();//Save on CPU seconds since we are doing async
                //Get the token and invoke get new data
                await SyncPushChanges.initUpdate(true);
                deferal.Complete();
            }

有人知道为什么我的方法会被多次调用吗?非常感谢您的帮助

我自己找到了解决办法。 问题是我没有取消注册我的后台代理,这导致它在更新应用程序时不正确工作(在这种情况下,当我再次从 VS2013 运行 应用程序时) . 以下代码发挥了作用

BackgroundExecutionManager.RemoveAccess();
            //Unregister the Background Agent 
            var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(keyval => keyval.Value.Name == "myAgent");
            if (entry.Value != null)
            {
                entry.Value.Unregister(true);
            }