2016-03-17 104 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