gpt4 book ai didi

c# - UWP OnBackgroundActivated,找不到合适的方法来覆盖

转载 作者:行者123 更新时间:2023-11-30 21:45:46 25 4
gpt4 key购买 nike

我正在尝试使用新的“单进程模型后台事件”API 来支持我的应用程序的后台任务。但是,我得到的是,在“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();
}
}

最佳答案

这里的问题是您试图覆盖 MainPage 类中的 OnBackgroundActivated 方法。 MainPage 类派生自 Page Class , 但是 Application.OnBackgroundActivated methodApplication class 的一种方法这在 Page 类中不存在,因此您得到了 找不到合适的方法来覆盖 错误。

要解决此问题,我们需要将 OnBackgroundActivated 方法放在 App 类中,例如:

sealed partial class App : Application
{
/// <summary>
/// Override the Application.OnBackgroundActivated method to handle background activation in
/// the main process. This entry point is used when BackgroundTaskBuilder.TaskEntryPoint is
/// not set during background task registration.
/// </summary>
/// <param name="args"></param>
protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
//TODO
}
}

关于单进程后台任务的更多信息,请参见Support your app with background tasksBackground activity with the Single Process Model .

关于c# - UWP OnBackgroundActivated,找不到合适的方法来覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39833457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com