Press "Enter" to skip to content

Building a Task Sequence in Xamarin Forms/ MAUI (Part 2)

In the first part of this series, we covered how to build a task sequence. In this part I’m going to show you how to start the sequence from a specific task, run tasks conditionally and pass parameters between tasks.

Starting from an specific task

There are scenarios where we want the task sequence to start from a task. For example, you want to start in the HomePage and skip all the previous tasks.

To achieve it, create an overload for StartAsync method of the StartupTaskSequencer class that will take an IStartupTask as parameter. This task will be the starting task in the sequence, so all the tasks previous to this one will be skipped.

In the SplashPage, when calling the StartAsync method specify which task we want to start from.

Result:

Run tasks conditionally

Is very common to run tasks only if it meets certain conditions. For example, if you have an application that shows an advertisement task only if the user is using the free version otherwise this task should be skipped.

To represent this scenario, let’s add a property for the version type in the App class.

In the AdvertisingPage by overriding the CanRunAsync method, evaluate if VersionType is free. If so task should run, otherwise will be skipped.

Passing parameters

In certain use cases we might need the output of the previous task to be the input of the next task to run. For example, you want to pass the username from the LoginPage task to the HomePage task.

To achieve it, let’s create a class to represent parameters:

Modify the IStartupTask to support parameters:

Modify StartupTaskSequencer class to pass parameters of previous task to the next running task

Pass the parameters in the CompleteAsync method of the previous task:

Evaluate and consume the parameters in the next task.

Result:

Stay tuned for the next part of this series where you’ll see how to build a task sequence using Prism.

Check the full source code here.

Happy Task Sequence!