当前位置: 首页>>代码示例>>C#>>正文


C# Background.BackgroundTaskBuilder类代码示例

本文整理汇总了C#中Windows.ApplicationModel.Background.BackgroundTaskBuilder的典型用法代码示例。如果您正苦于以下问题:C# BackgroundTaskBuilder类的具体用法?C# BackgroundTaskBuilder怎么用?C# BackgroundTaskBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


BackgroundTaskBuilder类属于Windows.ApplicationModel.Background命名空间,在下文中一共展示了BackgroundTaskBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RegisterTasks

        private async Task RegisterTasks()
        {
            // Applications must have lock screen privileges in order to receive raw notifications
            var backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();

            // Make sure the user allowed privileges
            if (backgroundStatus != BackgroundAccessStatus.Denied && backgroundStatus != BackgroundAccessStatus.Unspecified)
            {
                var systemEventTaskBuilder = new BackgroundTaskBuilder();
                var systemEventTrigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
                systemEventTaskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
                systemEventTaskBuilder.SetTrigger(systemEventTrigger);

                // Background tasks must live in separate DLL, and be included in the package manifest
                // Also, make sure that your main application project includes a reference to this DLL
                systemEventTaskBuilder.TaskEntryPoint = SystemEventTaskEntryPoint;
                systemEventTaskBuilder.Name = SystemEventTaskName;

                try
                {
                    var task = systemEventTaskBuilder.Register();
                    task.Completed += this.SystemEventBackgroundTaskCompleted;
                }
                catch (Exception ex)
                {
                    // Should probably log this somewhere...
                }
            }
            else
            {
                // Should probably log this somewhere...
            }
        }
开发者ID:gep13,项目名称:WindowsPhoneSample,代码行数:33,代码来源:BackgroundTaskService.cs

示例2: RegisterBackgroundTask

        /// <summary>
        /// Register a background task with the specified taskEntryPoint, name, trigger,
        /// and condition (optional).
        /// </summary>
        /// <param name="taskEntryPoint">Task entry point for the background task.</param>
        /// <param name="name">A name for the background task.</param>
        /// <param name="trigger">The trigger for the background task.</param>
        /// <param name="condition">An optional conditional event that must be true for the task to fire.</param>
        public static BackgroundTaskRegistration RegisterBackgroundTask(String taskEntryPoint, String name)
        {
            BackgroundTaskRegistration task = null;
            try
            {
                var builder = new BackgroundTaskBuilder();

                builder.Name = name;
                builder.TaskEntryPoint = taskEntryPoint;
                DeviceUseTrigger de = new DeviceUseTrigger();
               // de.RequestAsync();
            //    builder.SetTrigger(new TimeTrigger(15, true));
                task = builder.Register();
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception while registering background task: " + e.Message);
            }
            UpdateBackgroundTaskStatus(name, true);

            //
            // Remove previous completion status from local settings.
            //
            var settings = ApplicationData.Current.LocalSettings;
            settings.Values.Remove(name);

            return task;
        }
开发者ID:flecoqui,项目名称:Windows10,代码行数:36,代码来源:AudioDiscoveryTaskManager.cs

示例3: RegisterCortanaIntegrationTask

      /// <summary>
      /// Registers the background task that makes cortana listen to defined keywords
      /// </summary>
      private void RegisterCortanaIntegrationTask()
      {
         // Tracks whether the background task has been registerd or not.
         bool taskRegistered = false;
         string taskName = "CortanaMake";

         // Search background tasks registrations for the task name
         foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> task in BackgroundTaskRegistration.AllTasks)
         {
            if (task.Value.Name == taskName)
            {
               taskRegistered = true;
               break;
            }
         }

         // Register the task if it hasn't already been registered
         if(!taskRegistered)
         {
            BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
            builder.Name = taskName;
            builder.TaskEntryPoint = "BackgroundComponent"; // This is the name of a background task that needs to get created.  When the task is run, 
                                                            // the public void Run(IBackgroundTaskInstance taskInstance) will be called in this library.  
                                                            // We also have to register this in our app manifest.
            builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
            BackgroundTaskRegistration task = builder.Register();
            task.Completed += new BackgroundTaskCompletedEventHandler(OnCortanaMakeTaskCompleted);
         }
      }
开发者ID:sparker2000,项目名称:Make,代码行数:32,代码来源:MainPage.xaml.cs

示例4: PushNotifications

        /// <summary>
        /// The register task.
        /// </summary>
        private static async void PushNotifications()
        {
            const string PushNotificationTaskName = "ToastNotifications";

            if (GetRegisteredTask(PushNotificationTaskName) != null)
            {
                return;
            }

            var ns = new NotificationStore();
            ns.Register();

            await BackgroundExecutionManager.RequestAccessAsync();

            await ObtainLockScreenAccess();
            var taskBuilder = new BackgroundTaskBuilder
                                  {
                                      Name = PushNotificationTaskName,
                                      TaskEntryPoint = typeof(PushNotificationTask).FullName
                                  };

            var trigger = new PushNotificationTrigger();
            taskBuilder.SetTrigger(trigger);

            var internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
            taskBuilder.AddCondition(internetCondition);

            try
            {
                taskBuilder.Register();
            }
            catch (Exception exception)
            {
            }
        }
开发者ID:jlkalberer,项目名称:Linkslap.WP,代码行数:38,代码来源:RegisterTasks.cs

示例5: StartInBackground_Click

        private async void StartInBackground_Click(object s, RoutedEventArgs e)
        {
            var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
            var geofenceTaskBuilder = new BackgroundTaskBuilder
            {
                Name = "GeofenceBackgroundTask",
                TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask"
            };

            var trigger = new LocationTrigger(LocationTriggerType.Geofence);
            geofenceTaskBuilder.SetTrigger(trigger);
            var geofenceTask = geofenceTaskBuilder.Register();
            geofenceTask.Completed += (sender, args) =>
            {
                var geoReports = GeofenceMonitor.Current.ReadReports();
                foreach (var geofenceStateChangeReport in geoReports)
                {
                    var id = geofenceStateChangeReport.Geofence.Id;
                    var newState = geofenceStateChangeReport.NewState.ToString();
                    Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        new MessageDialog(newState + " : " + id)
                        .ShowAsync());
                }
            };
        }
开发者ID:nagendraksrivastava,项目名称:GeofencingWP81,代码行数:25,代码来源:MainPage.xaml.cs

示例6: OnNavigatedTo

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            if (e.Parameter is int)
            {
                Pivot.SelectedIndex = (int) e.Parameter;
            }
            LiveTileToggle.IsEnabled = App.MainViewModel.SignOnSaved;
            NotificationsToggle.IsEnabled = App.MainViewModel.SignOnSaved;
            VerifyIdToggle.IsEnabled = App.MainViewModel.SignOnSaved;

            LiveTileToggle.IsOn = (bool) (localSettings[App.RL.GetString("TileSettingString")] ?? false);
            NotificationsToggle.IsOn = (bool) (localSettings[App.RL.GetString("NotificationSettingString")] ?? false);
            VerifyIdToggle.IsOn = (bool)(localSettings[App.RL.GetString("VerifyIdSettingString")] ?? false);

            if (App.MainViewModel.SignOnSaved)
            {
                string taskName = App.RL.GetString("BackgroundTaskName");
                if (!IsTaskRegistered(taskName))
                {
                    var builder = new BackgroundTaskBuilder();
                    builder.Name = taskName;
                    builder.TaskEntryPoint = "BackgroundUpdater.BalanceCheck";
                    builder.SetTrigger(new TimeTrigger(15, false));
                    //builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); //this doesn't work...

                    await BackgroundExecutionManager.RequestAccessAsync();

                    BackgroundTaskRegistration task = builder.Register();

                }
            }

            base.OnNavigatedTo(e);
        }
开发者ID:parkerreno,项目名称:FoodTile,代码行数:34,代码来源:Settings.xaml.cs

示例7: registerBackgroundTask

        public async void registerBackgroundTask()
        {
            try
            {
                var result = await BackgroundExecutionManager.RequestAccessAsync();
                if (result == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                    result == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
                {
                    foreach (var task in BackgroundTaskRegistration.AllTasks)
                    {
                        if (task.Value.Name == TASK_NAME)
                            task.Value.Unregister(true);
                    }

                    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                    builder.Name = TASK_NAME;
                    builder.TaskEntryPoint = TASK_ENTRY;
                    builder.SetTrigger(new TimeTrigger(15, false));
                    var registration = builder.Register();
                }
                for (int i = 0; i < 5; i++)
                {
                    Uri u = new Uri("ms-appx:///tile/TileTemplate" + new Random().Next(1, 3).ToString() + ".xml");
                    StorageFile xmlFile = await StorageFile.GetFileFromApplicationUriAsync(u);
                    XmlDocument doc = await XmlDocument.LoadFromFileAsync(xmlFile);
                    TileNotification notifi = new TileNotification(doc);
                    TileUpdater udt = TileUpdateManager.CreateTileUpdaterForApplication();
                    udt.Update(notifi);
                }
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
        }
开发者ID:DXChinaTE,项目名称:BankApp,代码行数:35,代码来源:Register.cs

示例8: RegisterBackgroundTasks

        private async void RegisterBackgroundTasks()
        {
            var builder = new BackgroundTaskBuilder
            {
                Name = "BackgroundUpdateReadingList",
                TaskEntryPoint = "EasyReader.BackgroundTasks.UpdateReadingList"
            };

            try
            {
                await BackgroundExecutionManager.RequestAccessAsync();
            }
            catch (Exception)
            {
                
            }

            IBackgroundTrigger trigger = new TimeTrigger(15, true);
            builder.SetTrigger(trigger);

            IBackgroundCondition condition = new SystemCondition(SystemConditionType.InternetAvailable);
            builder.AddCondition(condition);

            IBackgroundTaskRegistration task = builder.Register();

            task.Progress += task_Progress;
            task.Completed += task_Completed;
        }
开发者ID:beaugunderson,项目名称:EasyReader,代码行数:28,代码来源:App.xaml.cs

示例9: RegisterTask

        private async void RegisterTask()
        {
            var access = await BackgroundExecutionManager.RequestAccessAsync();
            if (access == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
            {
                // Create a new background task builder.
                BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();

                // Create a new Phone Trigger.
                PhoneTrigger trigger = new PhoneTrigger(PhoneTriggerType.CallOriginDataRequest, false);

                // Associate the phone trigger with the background task builder.
                taskBuilder.SetTrigger(trigger);

                // Specify the background task to run when the trigger fires.
                taskBuilder.TaskEntryPoint = MainPage.BackgroundTaskEntryPoint;

                // Name the background task.
                taskBuilder.Name = MainPage.BackgroundTaskName;

                // Register the background task.
                BackgroundTaskRegistration taskRegistration = taskBuilder.Register();

                UpdateBackgroundTaskUIState(true);
                rootPage.NotifyUser("Registered Caller ID background task", NotifyType.StatusMessage);
            }
            else
            {
                rootPage.NotifyUser("Background agent access denied", NotifyType.ErrorMessage);
            }
        }
开发者ID:RasmusTG,项目名称:Windows-universal-samples,代码行数:31,代码来源:Scenario1_Register.xaml.cs

示例10: UpdateTile

        private async Task UpdateTile()
        {
            var result = await BackgroundExecutionManager.RequestAccessAsync();
            if (result == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                result == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
            {
                foreach (var task in BackgroundTaskRegistration.AllTasks)
                {
                    if (task.Value.Name == TASK_NAME)
                        task.Value.Unregister(true);

                    //task.Value.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
                    //task.Value.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
                }

                BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                var condition = new SystemCondition(SystemConditionType.InternetAvailable);

                builder.Name = TASK_NAME;
                builder.AddCondition(condition);
                builder.TaskEntryPoint = TASK_ENTRY;
                builder.SetTrigger(new TimeTrigger(15, false));
                var registration = builder.Register();

                registration.Progress += new BackgroundTaskProgressEventHandler(task_Progress);
                registration.Completed += new BackgroundTaskCompletedEventHandler(task_Completed);

            }
        }
开发者ID:stavrianosy,项目名称:BudgetManagementAssistant,代码行数:29,代码来源:MainPage.xaml.cs

示例11: Register

        /// <summary>
        /// 屏幕点亮时执行该任务:UserPresent
        /// http://msdn.microsoft.com/library/windows/apps/windows.applicationmodel.background.systemtriggertype.aspx
        /// </summary>
        /// <param name="taskEntryPoint">后台任务名</param>
        public static async void Register(string taskEntryPoint)
        {
            BackgroundExecutionManager.RemoveAccess();
            var status = await BackgroundExecutionManager.RequestAccessAsync();
            if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.Denied)
            {
                await new MessageDialog("系统关闭了后台运行,请前往‘系统设置’进行设置").ShowAsync();
                return;
            }
            else
            {
                var taskReg = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(t => t.Name.Equals(LIVETILETASK)) as BackgroundTaskRegistration;

                if (taskReg == null)
                {
                    //注册任务
                    var taskBuilder = new BackgroundTaskBuilder
                    {
                        Name = LIVETILETASK,
                        TaskEntryPoint = taskEntryPoint
                    };

                    var trigger = new SystemTrigger(SystemTriggerType.UserPresent, false);
                    taskBuilder.SetTrigger(trigger);
                    taskBuilder.Register();
                }
                else
                {
                    return;
                }
            }
        }
开发者ID:hippieZhou,项目名称:OneUWP,代码行数:37,代码来源:BackgroundTaskManager.cs

示例12: RegisterBackGroundTask

        public static BackgroundTaskRegistration RegisterBackGroundTask(string taskEntryPoint,
                                                                        string taskName,
                                                                        IBackgroundTrigger trigger,
                                                                        IBackgroundCondition condition)
        {
            //Check if its alterady Registrated
            foreach (var cur in BackgroundTaskRegistration.AllTasks)
            {
                if (cur.Value.Name == taskName)
                {
                    return (BackgroundTaskRegistration)(cur.Value);
                }
            }

            //Register the Task
            var builder = new BackgroundTaskBuilder();
            builder.Name = taskName;
            builder.TaskEntryPoint = taskEntryPoint;

            if (trigger != null)
            {
                builder.SetTrigger(trigger);
            }

            if (condition != null)
            {
                builder.AddCondition(condition);
            }

            BackgroundTaskRegistration task = builder.Register();

            return task;
        }
开发者ID:ernestoguimaraes,项目名称:cortana,代码行数:33,代码来源:Util.cs

示例13: RegisterTask

        /// <summary>
        /// Method reponsible for registering the background task
        /// </summary>
        private async void RegisterTask()
        {
            var taskRegistered = Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks.Select(task => task.Value.Name == nameof(TileUpdaterTask)).Count() > 0;
            if (taskRegistered)
            {
                this.StatusLabel.Text = "Task already registered!";
            }
            else
            {
                // Check if task is registred
                BackgroundAccessStatus allowed = await BackgroundExecutionManager.RequestAccessAsync();

                if (allowed == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || allowed == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
                {
                    var task = new BackgroundTaskBuilder
                    {
                        Name = nameof(TileUpdaterTask),
                        CancelOnConditionLoss = false,
                        TaskEntryPoint = typeof(TileUpdaterTask).ToString(),
                    };

                    task.SetTrigger(new TimeTrigger(15, false));
                    task.AddCondition(new SystemCondition(SystemConditionType.UserPresent));
                    task.Register();                    

                    this.StatusLabel.Text = "Task has been registered!";
                }
                else
                {
                    this.StatusLabel.Text = "Not allowd to register task!";
                }                
            }
        }
开发者ID:talkitbr,项目名称:TDC2015,代码行数:36,代码来源:MainPage.xaml.cs

示例14: RegisterBackgroundTask

        /// <summary>
        /// We proceed to register the task if it hasn't been registered yet. Otherwise, we fetch the registration
        /// and retrieve the ApplicationTrigger from there.
        /// </summary>
        private async void RegisterBackgroundTask()
        {
            try
            {
                if (!BackgroundTaskRegistration.AllTasks.Any(reg => reg.Value.Name == TileRegistrationName))
                {
                    // Configure task parameters
                    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
                    builder.TaskEntryPoint = typeof(TileUpdateTask).FullName;
                    builder.Name = TileRegistrationName;

                    // Remember to set an ApplicationTrigger so we can run it on demand later
                    this.backgroundTrigger = new ApplicationTrigger();
                    builder.SetTrigger(backgroundTrigger);
                    builder.Register();

                    MessageDialog infoDialog = new MessageDialog("Background task successfully registered.", "Info");
                    await infoDialog.ShowAsync();
                }
                else
                {
                    // Fetch registration details and trigger if already existing
                    var registration = BackgroundTaskRegistration.AllTasks.FirstOrDefault(reg => reg.Value.Name == TileRegistrationName).Value as BackgroundTaskRegistration;
                    this.backgroundTrigger = registration.Trigger as ApplicationTrigger;

                    MessageDialog infoDialog = new MessageDialog("Background task registration data successfully retrieved.", "Info");
                    await infoDialog.ShowAsync();
                }
            }
            catch (Exception ex)
            {
                MessageDialog errorDialog = new MessageDialog("There was an error while trying to register the background task.", "Error");
                errorDialog.ShowAsync();
            }
        }
开发者ID:r2d2rigo,项目名称:UWP-Samples,代码行数:39,代码来源:MainPage.xaml.cs

示例15: RegisterApplicationTrigger

        public static async Task RegisterApplicationTrigger(Type backgroundTaskType)
        {
            var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();

            if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
                backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
            {
                var appTrigger = new ApplicationTrigger();
                string taskEntryPoint = backgroundTaskType.ToString();
                var backgroundTaskBuilder = new BackgroundTaskBuilder
                {
                    Name = backgroundTaskType.Name,
                    TaskEntryPoint = taskEntryPoint
                };

                backgroundTaskBuilder.SetTrigger(appTrigger);

                var task = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(i => i.Name.Equals(backgroundTaskType.Name));
                task?.Unregister(true);
                backgroundTaskBuilder.Register();

                ValueSet message = new ValueSet { { "Command", "Init" } };

                var result = await appTrigger.RequestAsync(message);

                if (result == ApplicationTriggerResult.Allowed)
                {
                    Debug.WriteLine(string.Format("{0} called!", backgroundTaskType.Name));
                }
            }
        }
开发者ID:sanjayasl,项目名称:Events,代码行数:31,代码来源:BackgroundTaskHelper.cs


注:本文中的Windows.ApplicationModel.Background.BackgroundTaskBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。