How to crash other WP7 applications?
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:
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!
Oh yeah, I fixed this in my app several months ago – I use as I call it “Safe handler”, which captures all unwanted double tap exceptions
SmsComposeTask composeSMS = new SmsComposeTask
{
Body = message,
To = number
};
SafeShow(composeSMS.Show);
public static void SafeShow(Action action)
{
try
{
action();
}
catch (InvalidOperationException e)
{
// TODO log
// catch possible Navigation exception for doubletaps
}
}
It’s always good practice to have a TRY/CATCH when sending apps out into the world. People don’t like when programs don’t work but they really don’t like it when it crashes. It also makes you look bad when you don’t child proof the application.
I got a an address button which launches the bing map and also an Ad control at the top of the page.
When I click the address button and before it launches, quickly click on the Ad, the app crashes. Is there a workaround for this?
Note: When I click the Ad first and then the button nothing bad happens
Hello there! I know this is kinda off topic however I’d figured I’d ask.
Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
My website covers a lot of the same topics as yours
and I believe we could greatly benefit from each
other. If you might be interested feel free to
send me an e-mail. I look forward to hearing from you!
Great blog by the way!