2016-03-17 102 views
0

我想有一個時間推出舉杯顯示的過程按鈕觸發顯示與按鍵舉杯後臺任務。 「你準備好做xyx了嗎?」 [是] [否]。點擊是,然後轉到網頁。否,將關閉烤麪包片直到下一個時間段。UWP如何在從後臺任務

在Windows 10通用應用程序中,我看到了一個示例,其中通用應用程序中帶按鈕的按鈕已啓動並且後臺任務中處理按鈕。

我見過敬酒,當從沒有按鈕的後臺任務啓動吐司時,會顯示一條消息。

我需要知道的是,如果從後臺任務啓動帶按鈕的烤麪包,如何處理按鈕。

這可能嗎?如果是這樣如何?

這裏是用在後臺任務,以顯示敬酒代碼:

namespace BGtask 
{ 
    public sealed class BGTimerTask : IBackgroundTask 
    { 
     public void Run(IBackgroundTaskInstance taskInstance) 
     { 
      var deferral = taskInstance.GetDeferral(); 

      try 
      { 
       SendToast(); 
      } 
      finally 
      { 
       // And finally release the deferral since we're done 
       deferral.Complete(); 
      } 
     } 

     private void SendToast() 
     { 
      ToastContent content = new ToastContent() 
      { 
       Visual = new ToastVisual() 
       { 
        TitleText = new ToastText() 
        { 
         Text = "XYZ" 
        }, 

        BodyTextLine1 = new ToastText() 
        { 
         Text = "Are you ready to do xyz?" 
        } 
       }, 

       Actions = new ToastActionsCustom() 
       { 
        Inputs = 
        { 
         new ToastSelectionBox("selection") 
         { 
          Items = 
          { 
           new ToastSelectionBoxItem("1", "Yes"), 
           new ToastSelectionBoxItem("2", "Not Now"), 
           new ToastSelectionBoxItem("3", "Don't Show Again") 
          }, 
          DefaultSelectionBoxItemId = "1" 
         } 
        }, 
        Buttons = 
        { 

         new ToastButton("OK", new QueryString() 
         { 
          { "action", "ok" }, 

         }.ToString()) 
         { 
          ActivationType = ToastActivationType.Background 
         }, 


        } 
       } 
      }; 

      ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(content.GetXml())); 

     } 
    } 
} 
+1

告訴你嘗試了一些假的代碼? – Moumit

+0

已添加代碼,但問題是如何處理此麪包中的按鈕。 – Jack

回答

1

我明白了,你已經設置ToastActivationType.Background。這是正確的設置。您將需要處理來自同一應用程序註冊的另一個後臺任務的按鈕:

public sealed class NotificationActionBackgroundTask : IBackgroundTask 
{ 
    public void Run(IBackgroundTaskInstance taskInstance) 
    { 
     var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail; 

     if (details != null) 
     { 
      string arguments = details.Argument; // button argument 
      var userInput = details.UserInput; 
      var selection = userInput["selection"] // dropdown value 

      // process button 
     } 
    } 
} 

你需要用ToastNotificationActionTrigger註冊該任務:

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync(); 

BackgroundTaskBuilder builder = new BackgroundTaskBuilder() 
{ 
    Name = "MyToastTask", 
    TaskEntryPoint = "BgTask.NotificationActionBackgroundTask" 
}; 

builder.SetTrigger(new ToastNotificationActionTrigger()); 

BackgroundTaskRegistration registration = builder.Register(); 

當然,不要忘了申報在應用程序清單的任務(你需要檢查系統事件),並引用包含從應用程序的任務庫。

欲瞭解更多詳情,你可以閱讀this tutorial使用Windows 10自適應模板從烤麪包通知處理背景激活應該是您特別感興趣的。

+0

完美運作。我沒有意識到你可以在應用程序清單中創建第二個指向第二個後臺任務的條目,只要我的代表足夠高,我就會添加upvote。謝謝! – Jack

+0

@Jack我很高興,它解決了你的問題。如果它適合你,你可以[接受答案](http://meta.stackexchange.com/a/5235/174150)。你也會從中獲得一些聲譽。 –

+0

剛剛做到了。謝謝 – Jack