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


C# BackgroundTaskBuilder.SetTrigger方法代码示例

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


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

示例1: addBackgroundTask

        /// <summary>
        /// 增加后台任务
        /// </summary>
        private async void addBackgroundTask()
        {
            bool taskRegistered = false;
            //判断是否已经注册过了
            taskRegistered = BackgroundTaskRegistration.AllTasks.Any(x => x.Value.Name == exampleTaskName);

            if (!taskRegistered)
            {
                BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
                var builder = new BackgroundTaskBuilder();
                builder.Name = exampleTaskName;
                builder.TaskEntryPoint = "Tasks.LetterBackgroundTask";
                //后台触发器,可多个
                builder.SetTrigger(new SystemTrigger(SystemTriggerType.NetworkStateChange, false));
                builder.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, false));
                //builder.SetTrigger(new MaintenanceTrigger(15, false)); //定时后台任务
                builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));

                BackgroundTaskRegistration task = builder.Register();
            }
            else
            {
                var cur = BackgroundTaskRegistration.AllTasks.FirstOrDefault(x => x.Value.Name == exampleTaskName);
                BackgroundTaskRegistration task = (BackgroundTaskRegistration)(cur.Value);
            }

        }
开发者ID:cappuccinoqi,项目名称:Date_Windows,代码行数:30,代码来源:App.xaml.cs

示例2: RegisterBtn_Click

        // Example of a Background Task, not tested
        private async void RegisterBtn_Click(object sender, RoutedEventArgs e)
        {
            string myTaskName = "First Task";

            // check if task is already registered
            foreach (var cur in BackgroundTaskRegistration.AllTasks)
            {
                if (cur.Value.Name == myTaskName)
                {
                    Log("Task already registered", "Caution");
                    return;
                }
            }

            // Windows Phone app must call this to use trigger types (see MSDN)
            await BackgroundExecutionManager.RequestAccessAsync();

            // register a new task
            BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "First Task", TaskEntryPoint = "WebServerTask.BackgroundTask" };

            taskBuilder.SetTrigger(new TimeTrigger(15, true));
            //taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
            
            BackgroundTaskRegistration myFirstTask = taskBuilder.Register();

            await (new MessageDialog("Task registered")).ShowAsync();
        }
开发者ID:nudaca,项目名称:simplehttpserver.uwp,代码行数:28,代码来源:MainPage.xaml.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: App

        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {

            this.InitializeComponent();
            this.Suspending += OnSuspending;


            var taskRegistered = false;

            foreach (var task in Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks)
            {
                if (task.Value.Name == "BaconographyW8BackgroundTaskLiveTileUpdater")
                {
                    taskRegistered = true;
                    break;
                }
            }

            if (!taskRegistered)
            {
                var builder = new BackgroundTaskBuilder();

                builder.Name = "BaconographyW8BackgroundTaskLiveTileUpdater";
                builder.TaskEntryPoint = "BaconographyW8BackgroundTask.LiveTileUpdater";
                builder.SetTrigger(new TimeTrigger(15, false));
                builder.Register();
            }
        }
开发者ID:Synergex,项目名称:Baconography,代码行数:32,代码来源:App.xaml.cs

示例5: RegisterPushNotifications

        /// <summary>
        /// The register push notifications.
        /// </summary>
        private static async void RegisterPushNotifications()
        {
            const string RegisterPushNotificationName = "RegisterNotifications";
            const int RegistrationInterval = 10 * 24 * 60;

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

            await BackgroundExecutionManager.RequestAccessAsync();

            var taskBuilder = new BackgroundTaskBuilder
                                  {
                                      Name = RegisterPushNotificationName,
                                      TaskEntryPoint = typeof(RegisterPushNotificationTask).FullName
                                  };

            var trigger = new TimeTrigger(RegistrationInterval, false);
            taskBuilder.SetTrigger(trigger);

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

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

示例6: 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, IBackgroundTrigger trigger, IBackgroundCondition condition)
        {
            var builder = new BackgroundTaskBuilder();

            builder.Name = name;
            builder.TaskEntryPoint = taskEntryPoint;
            builder.SetTrigger(trigger);

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

            BackgroundTaskRegistration task = builder.Register();

            UpdateBackgroundTaskStatus(name, true);

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

            return task;
        }
开发者ID:AlyCrunch,项目名称:samples,代码行数:33,代码来源:Constants.cs

示例7: StartLocationTracker

    public void StartLocationTracker()
    {
     if( CanRunAsTask() )
      {
        BackgroundTaskBuilder geolocTaskBuilder = new BackgroundTaskBuilder();

        geolocTaskBuilder.Name = SampleBackgroundTaskName;
        geolocTaskBuilder.TaskEntryPoint = SampleBackgroundTaskEntryPoint;

        // Create a new timer triggering at a 15 minute interval
        var trigger = new TimeTrigger( UpdateInterval, true );

        // Associate the timer trigger with the background task builder
        geolocTaskBuilder.SetTrigger( trigger );

        // Register the background task
        _geolocTask = geolocTaskBuilder.Register();

        // Associate an event handler with the new background task
        _geolocTask.Completed += new BackgroundTaskCompletedEventHandler( OnCompleted );
      }
      else
      {
         task = new LocationBackgroundTask();
        timer = ThreadPoolTimer.CreatePeriodicTimer( TimerElapsed, new TimeSpan( TimeSpan.TicksPerMillisecond * 30000 ) );
     }
    }
开发者ID:Georotzen,项目名称:.NET-SDK-1,代码行数:27,代码来源:Win8LocationTrackerEngine.cs

示例8: RegisterTaskButton_Click

        private void RegisterTaskButton_Click(Object sender, RoutedEventArgs e)
        {
            if (GetRegisteredTask() == null)
            {
                BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
                MaintenanceTrigger trigger = new MaintenanceTrigger(MAINTENANCE_INTERVAL, false);
                taskBuilder.SetTrigger(trigger);
                taskBuilder.TaskEntryPoint = PUSH_NOTIFICATIONS_TASK_ENTRY_POINT;
                taskBuilder.Name = PUSH_NOTIFICATIONS_TASK_NAME;

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

                try
                {
                    taskBuilder.Register();
                    rootPage.NotifyUser("Task registered", NotifyType.StatusMessage);
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Error registering task: " + ex.Message, NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUser("Task already registered", NotifyType.ErrorMessage);
            }
        }
开发者ID:WHISHWORKS,项目名称:MuleSummit2013,代码行数:28,代码来源:ScenarioInput2.xaml.cs

示例9: 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

示例10: 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

示例11: 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

示例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: 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


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