Wensus Analytics

Caliburn.Micro v1.1: Configuring launchers and choosers

Caliburn.Micro v1.1 offers a completely new way of executing Windows Phone 7 launchers and choosers. The new version takes advantage of the EventAggregator-class when executing the tasks. For example if you want to execute the MarketPlaceReviewTask, it’s now as simple as calling the following code:

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">            eventAggregator.RequestTask&lt;MarketplaceReviewTask&gt;();

But how about if you have to configure the task before running it? It’s a common scenario that the WebBrowserTask is configured with the website’s URL or that the MarketplaceSearchTask is filled with some keyword. In previous versions one used the IConfigureTask to set the parameters. But now things are much simpler: The configuration action can be given as the parameter to the RequestTask’s method. You have two options: 1. Implicitly declaring the configuration method or 2. Lambda expressions. Let’s take a look at both options.

1. Configuration method

Here’s a code example where the EmailComposeTask is executed and the configuration happens in a implicitly declared method:

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Contact()
        {
            eventAggregator.RequestTask<EmailComposeTask>(ConfigureContactTask);
        }

        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> ConfigureContactTask(EmailComposeTask emailComposeTask)
        {
            emailComposeTask.To = &quot;<span style="color: #8b0000">info@site.org</span>&quot;;
            emailComposeTask.Subject = &quot;<span style="color: #8b0000">Comment from app</span>&quot;;
        }

2. Lambda expression

The RequestTask-methd takes System.Action as its parameters so the above code could be written without the ConfigureContactTask-method. Here’s an example::

<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Contact()
        {
            eventAggregator.RequestTask<EmailComposeTask>(x =>
                                                              {
                                                                  x.To = &quot;<span style="color: #8b0000">info@site.org</span>&quot;;
                                                                  x.Subject = &quot;<span style="color: #8b0000">Comment from app</span>&quot;;
                                                              });
        }