Thursday, July 30, 2015

Unexpected Windows Phone errors

I've been updating an app that targets WP8.0.
With a couple of hours spare I've been looking through the dev center health reports to see if there are any exceptions occurring that I can do anything about.
It seems that I'm not be defensive enough in my coding and previously didn't allow for things that should never happen from happening. :S

Yes, really.

Even if it's not displayed an app should always have one tile, the "primary tile" that can be accessed in code so it can be updated.
Like this:

var primaryTile = ShellTile.ActiveTiles.First();

Except when it can't.

According to the stack traces, the above has failed.
The lesson? Always use `FirstOrDefault()` even when there should always be at least one and you want the first.


A page has a State dictionary that can be used for storing details while the app is deactivated/tombstoned.

You should always be able to access it and it should always exist:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
 
    if (this.State.Count > 0) 
    {

But then, apparently (according to the exception details) there may be an exception when accessing the `State` property.



For an app with over a million installs the number of times the above have happened is incredibly small but may be worth noting if you want to be super robust in your code. Of course, if you not busy with Windows 10 related things ;)


2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete