本文整理匯總了C#中Windows.ApplicationModel.Background.BackgroundTaskBuilder.AddCondition方法的典型用法代碼示例。如果您正苦於以下問題:C# BackgroundTaskBuilder.AddCondition方法的具體用法?C# BackgroundTaskBuilder.AddCondition怎麽用?C# BackgroundTaskBuilder.AddCondition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Windows.ApplicationModel.Background.BackgroundTaskBuilder
的用法示例。
在下文中一共展示了BackgroundTaskBuilder.AddCondition方法的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);
}
}
示例2: RegisterBackgroundTask
/*
* 在 Windows Phone 上,你必須在嘗試注冊任何後台任務之前調用 RequestAccessAsync
*/
/// <summary>
/// 注冊後台任務
/// </summary>
/// <param name="taskEntryPoint">任務的入口點</param>
/// <param name="name">任務名稱</param>
/// <param name="trigger">輪詢時間</param>
/// <param name="condition">係統事件</param>
/// <returns></returns>
public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
{
BackgroundTaskRegistration task = null;
if (TaskRequiresBackgroundAccess(name))
{
await BackgroundExecutionManager.RequestAccessAsync();
}
var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
{
var builder = new BackgroundTaskBuilder();
builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
if (condition != null)
{
builder.AddCondition(condition);
builder.CancelOnConditionLoss = true;
}
task = builder.Register();
}
return task;
}
示例3: 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;
}
示例4: 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);
}
}
示例5: 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...
}
}
示例6: 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)
{
}
}
示例7: RegisterBackgroundTasks
private async void RegisterBackgroundTasks()
{
var access = await BackgroundExecutionManager.RequestAccessAsync();
if (access == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || access == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
{
TimeTrigger taskTrigger = new TimeTrigger(15, false);
foreach (var _task in BackgroundTaskRegistration.AllTasks)
{
if (_task.Value.Name == "WPGetListUpdateTimer" || _task.Value.Name=="WPGetListUpdateInternet")
{
_task.Value.Unregister(true);
}
}
SystemTrigger striger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
var bgTaskBuilder = new BackgroundTaskBuilder();
bgTaskBuilder.Name = "WPGetListUpdateTimer";
bgTaskBuilder.TaskEntryPoint = "BackgroundTaskWinMD.WPGetListUpdateBackgroundTask";
bgTaskBuilder.SetTrigger(taskTrigger);
// условие, согласно которому триггер будет выполнен только если интернет доступен
SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
bgTaskBuilder.AddCondition(internetCondition);
BackgroundTaskRegistration task = bgTaskBuilder.Register();
bgTaskBuilder = new BackgroundTaskBuilder();
bgTaskBuilder.Name = "WPGetListUpdateInternet";
bgTaskBuilder.TaskEntryPoint = "BackgroundTaskWinMD.WPGetListUpdateBackgroundTask";
bgTaskBuilder.SetTrigger(striger);
bgTaskBuilder.AddCondition(internetCondition);
BackgroundTaskRegistration task2 = bgTaskBuilder.Register();
}
}
示例8: RegisterBackgroundTask
public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
{
//always unregister for now.
foreach (var bgt in BackgroundTaskRegistration.AllTasks)
{
bgt.Value.Unregister(true);
}
//in UWP, you have to ask for access. Just like in WP
var access = await BackgroundExecutionManager.RequestAccessAsync();
if (access == BackgroundAccessStatus.Denied)
{
throw new UnauthorizedAccessException();
}
var builder = new BackgroundTaskBuilder();
builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
if (condition != null)
{
builder.AddCondition(condition);
builder.CancelOnConditionLoss = true;
}
BackgroundTaskRegistration task = builder.Register();
return task;
}
示例9: RegisterBackgroundTask
private async Task RegisterBackgroundTask()
{
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == UPDATE_TILE_TASK_NAME)
{
// 已經注冊後台任務。
return;
}
}
BackgroundAccessStatus access = await BackgroundExecutionManager.RequestAccessAsync();
if (access != BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
{
// 沒有權限。
return;
}
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
// 僅在網絡可用下執行後台任務。
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
builder.Name = UPDATE_TILE_TASK_NAME;
builder.TaskEntryPoint = "BingoWallpaper.BackgroundTask.UpdateTileTask";
// 每 90 分鍾觸發一次。
builder.SetTrigger(new TimeTrigger(90, false));
BackgroundTaskRegistration registration = builder.Register();
}
示例10: RegisterTaskAsync
public static IAsyncOperation<BackgroundTaskRegistration> RegisterTaskAsync()
{
return AsyncInfo.Run(async (cancellationToken) =>
{
if (IsTaskRegistered())
return _current;
await BackgroundExecutionManager.RequestAccessAsync();
//http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.background.timetrigger.aspx
IBackgroundTrigger trigger = new TimeTrigger(6*60, false); //6 hours
// Builds the background task.
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.Name = FriendlyName;
builder.TaskEntryPoint = typeof(BackgroundDemoTask).FullName;
builder.SetTrigger(trigger);
SystemCondition condition = new SystemCondition(SystemConditionType.InternetAvailable);
builder.AddCondition(condition);
// Registers the background task, and get back a BackgroundTaskRegistration object
// representing the registered task.
_current = builder.Register();
return _current;
});
}
示例11: 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;
}
示例12: BackRegister
public static async void BackRegister()
{
string taskName = "back_notifi"; //後台任務名稱
string entryPoint = "BackPush.NotifiBackTask"; //入口點
// 檢查是否許後台任務
var result = await BackgroundExecutionManager.RequestAccessAsync();
if (result == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
{
// 檢查是否已經注冊後台任務
var task = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault((t) => t.Name == taskName);
// 如果未注冊,則進行注冊
if (task == null)
{
BackgroundTaskBuilder tb = new BackgroundTaskBuilder();
tb.TaskEntryPoint = entryPoint;
tb.Name = taskName;
// 觸發器為推送通知觸發器
tb.SetTrigger(new PushNotificationTrigger());
// 運行條件為網絡可用
tb.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
// 注冊
tb.Register();
}
}
}
示例13: registerButton_Click
private async void registerButton_Click(object sender, RoutedEventArgs e)
{
TimeTrigger timetrigger = new TimeTrigger(30, false);
PushNotificationTrigger notificationTrigger = new PushNotificationTrigger();
var SampleTask = new BackgroundTaskBuilder(); //創建後台任務實例
SampleTask.Name = "Reminder"; //指定後台任務名稱
SampleTask.TaskEntryPoint = "ReminderComponent.Reminder";//指定後台任務名稱
SampleTask.SetTrigger(timetrigger);//指定後台任務的觸發器
SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
SampleTask.AddCondition(internetCondition);
var access = await BackgroundExecutionManager.RequestAccessAsync();
if (access == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
{
BackgroundTaskRegistration task = SampleTask.Register();
task.Progress += Value_Progress; ;
task.Completed += Value_Completed; ;
UpdateUI("", "注冊成功");
registerButton.IsEnabled = false;
cancelButtton.IsEnabled = true;
var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
settings.Values.Remove(task.Name);
}
else if (access == BackgroundAccessStatus.Denied)//用戶禁用後台任務或後台任務數量已達最大
{
await new MessageDialog("您已禁用後台任務或後台任務數量已達最大!").ShowAsync();
}
}
示例14: 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);
}
}
示例15: RegisterBackgroundTask
private async static Task<BackgroundTaskRegistration> RegisterBackgroundTask(Type taskEntryPoint, string taskName, IBackgroundTrigger trigger, IBackgroundCondition condition)
{
var status = await BackgroundExecutionManager.RequestAccessAsync();
if (status == BackgroundAccessStatus.Unspecified || status == BackgroundAccessStatus.Denied)
{
return null;
}
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == taskName)
{
cur.Value.Unregister(true);
}
}
var builder = new BackgroundTaskBuilder
{
Name = taskName,
TaskEntryPoint = taskEntryPoint.FullName
};
builder.SetTrigger(trigger);
if (condition != null)
{
builder.AddCondition(condition);
}
return builder.Register(); ;
}