whiteShadoww
whiteShadoww

Reputation: 81

UWP OnBackgroundActivated, no suitable method found to overrride

I'm trying to use the new 'Background activity with the Single Process Model' APIs to support my app with background task. But, I'm getting, 'no suitable method found to override' on the 'OnBackgroundActivated' method. What is wrong with my code?

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();
        }
    }

Upvotes: 2

Views: 2211

Answers (1)

Jay Zuo
Jay Zuo

Reputation: 15758

The problem here is that you are trying to override OnBackgroundActivated method in MainPage class. MainPage class is derived from Page Class, but Application.OnBackgroundActivated method is a method of Application class which is not existed in Page Class, so you got the no suitable method found to override error.

To fix this issue, we need to put OnBackgroundActivated method in App class like:

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
    }
}

For more info about single process background task, please see Support your app with background tasks and Background activity with the Single Process Model.

Upvotes: 4

Related Questions