Archive

Posts Tagged ‘WebBrowserTask’

How to crash other WP7 applications?

February 20, 2012 6 comments

Do you know what is easiest way to crash someone’s WP7 application? Just quickly double-tap it’s “Rate my app” link or button. Marketplace Review page will display, but when you go back to app, it will disappear because it crashed.

Of course some app’s has this issue properly resolved, most not, but what happens when you double tap button or link that starts any kind of WP7 Launchers? Simply, your app will try twice to launch task, but second launch will throw “Navigation is not allowed when the task is not in the foreground” exception, because your app is not anymore active on phone.

Here is example. Create new Windows Phone application, uncomment ApplicationBar section at the end of MainPage.xaml and leave one ApplicationBarIconButton:

<phone:PhoneApplicationPage.ApplicationBar>
 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
 <shell:ApplicationBarIconButton IconUri="/Home.png" Text="navigate"/>
 </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar> 

In MainPage.xaml.cs add following code:

ApplicationBarIconButton appBarButton;

public MainPage()
{
    InitializeComponent();
    appBarButton = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    appBarButton.Click += new EventHandler(appBarButton_Click);
}

private void appBarButton_Click(object sender, EventArgs e)
{
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

This app will launch the web browser application and display the Bing web site. Start app and double-tap application bar icon button. Following unhandled exception will occur:

appbarcrash

This will happen in most situation where you are trying to start any of available launchers such as Web Browser Task, Share Link Task, Marketplace Review Task, etc. There are several ways to handle those situations:

1. Add task.Show() method in try/catch block:

private void appBarButton_Click(object sender, EventArgs e)
{
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    try {
        task.Show();
    }
    catch {}
}

2. Disable button on first click:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    appBarButton.Click += new EventHandler(appBarButton_Click);
    if ( e.NavigationMode == NavigationMode.Back )
        appBarButton.IsEnabled = true;
}

private void appBarButton_Click(object sender, EventArgs e)
{
    appBarButton.IsEnabled = false;
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

This will disable button, so second click won’t happen, but you also have to re-enable button when user goes back to this page.

3. Remove Click handler:

private void appBarButton_Click(object sender, EventArgs e)
{
    appBarButton.Click -= new EventHandler(appBarButton_Click);
    WebBrowserTask task = new WebBrowserTask();
    task.Uri = new Uri("http://www.bing.com");
    task.Show();
}

Happy coding!

Advertisements