0
我正在嘗試使用新的'單進程模型的後臺活動'API來支持我的應用程序與後臺任務。但是,我得到'OnBackgroundActivated'方法'找不到合適的方法來覆蓋'。我的代碼有什麼問題?UWP OnBackgroundActivated,找不到合適的方法覆蓋
public MainPage()
{
this.InitializeComponent();
Application.Current.EnteredBackground += Current_EnteredBackground;
}
private async void Current_EnteredBackground(object sender, Windows.ApplicationModel.EnteredBackgroundEventArgs e)
{
await RegisterBackgroundTask();
}
protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
// show a toast
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
}
private async Task RegisterBackgroundTask()
{
BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundAccessStatus == BackgroundAccessStatus.AllowedSubjectToSystemPolicy ||
backgroundAccessStatus == BackgroundAccessStatus.AlwaysAllowed)
{
foreach (var bgTask in BackgroundTaskRegistration.AllTasks)
{
if (bgTask.Value.Name == "MyTask")
{
bgTask.Value.Unregister(true);
}
}
var builder = new BackgroundTaskBuilder();
builder.Name = "MyTask";
builder.SetTrigger(new TimeTrigger(15, false));
// use builder.TaskEntryPoint if you want to not use the default OnBackgroundActivated
// we’ll register it and now will start work based on the trigger, here we used a Time Trigger
BackgroundTaskRegistration task = builder.Register();
}
}
謝謝,它的工作原理。從來沒有這樣想過。我對編程非常陌生,所以很難理解。謝謝! –