簡體   English   中英

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);
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM