Getting the right behaviors in your Phone 7 App – Part 2 Binding & Browsing
Dec
22
Written by:
12/22/2010 8:43 AM
In this example we will look at the WebBrowserTask and how a little bit of code can create a great design time experience for the times that you need to launch a browser in a Phone 7 app. Like the PhoneCallTask, the WebBrowserTask only contains one property. So, this example will look very similar to the one we looked at in Part 1. I will jump straight to the code as the concept will be identical to our previous example.
using System.Windows.Controls;
using System.Windows.Interactivity;
using Microsoft.Phone.Tasks;
namespace ITLackey.Phone7.Common.Behaviors
{
public class LaunchBrowserAction : TriggerAction<Control>
{
protected override void Invoke(object parameter)
{
WebBrowserTask task = new WebBrowserTask();
task.URL = this.AssociatedObject.Tag.ToString();
task.Show();
}
}
}
Again, we are simply using the Tag property of the associated control to pass in the data we need for our task. In this case it is the URL that we would like to direct the browser to when it is launched. Here is an example of using this behavior on a button:
<Button x:Name="FeedbackUrl"
Content="You may also submit feedback via the web"
Tag="{Binding SubmitFeedbackUrl}" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<ITLackey_Phone7_Common_Behaviors:LaunchBrowserAction/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Button>
Here we are just bound the SubmitFeedbackUrl to the Tag property of the button and dropped the behavior on. Done. That is all there is too it. Pretty simple example but now you can launch a browser session straight to the URL you want from any control in your Phone 7 app.
Enjoy!