Dmitry Lyalin's Applications for Windows Phone 7

Archive for August, 2011

ApplicationBar Errors–Dynamically adding buttons

Recently I ran into two errors around code I was writing to dynamically add buttons into the ApplicationBar. My application is targeted to Windows Phone 7.0 (NoDo) but my development tools are Mango Beta 2.

What I was trying to accomplish is add a ApplicationBarIconButton into my Main.xaml’s (XAML created) ApplicationBar, sounds simple right?

To do this I created the button at runtime and tried to add it into the “buttons” collection by calling .Add(). When I did this got the error below:

System.ArgumentException was unhandled
  Message=This list does not support that type

  StackTrace:
      at Microsoft.Phone.Shell.ApplicationBarItemList`1.System.Collections.IList.Add(Object value)

I thought maybe when this code executed mattered, so I tried it from both the Constructor of Main.xaml and the Loaded event, didn’t make a difference.

Next after asking around my peers and playing around with the API we discovered that NOT setting the Click handler until AFTER I add the button into the ApplicationBar avoided this error, crazy but it works.

Here what works:

Uri shareUriButton = new Uri("Images/Buttons/appbar.share.rest.png", UriKind.Relative);

ApplicationBarIconButton shareButton = new ApplicationBarIconButton(shareUriButton);
shareButton.Text = "Share";
shareButton.IsEnabled = true;
this.ApplicationBar.Buttons.Add(shareButton);
shareButton.Click += new EventHandler(buttonShare_Click);

Even though this resolved my immediate problem I decided to play around with the API some more and tried to add a button without setting the IconUri first and got another vague error, easy enough to avoid but I wanted it to be a searchable issue, here is the error you get:

System.ArgumentNullException was unhandled
Message=Cannot clear the icon while in a list
Parameter name: value

  StackTrace:
       at Microsoft.Phone.Shell.ApplicationBarItemContainer.set_IconUri(Uri value)
       at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.UpdateProperties(Object sender, BoolEventArgs args)
       at Microsoft.Phone.Shell.ApplicationBarItemContainer.BeginAttachToAppBar()
       at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.BeginAttachToAppBar()
       at Microsoft.Phone.Shell.ApplicationBarItemList`1.Insert(Int32 index, IApplicationBarIconButton item)
       at Microsoft.Phone.Shell.ApplicationBarItemList`1.Add(IApplicationBarIconButton item)
       at Microsoft.Phone.Shell.ApplicationBarItemList`1.System.Collections.IList.Add(Object value)

Recap

So to recap when adding an ApplicationBarIconButton into the ApplicationBar make sure to:

  • Specify a valid IconUri BEFORE you add the button into the ApplicationBar’s buttons collection
  • Set the “Click” event handler AFTER you add it to the ApplicationBar’s buttons collection

Hope this helps someone. I don’t know if this is an old issue or a new issue, this could perhaps even be fixed in the final version of the Mango developers tools but if not then your answer how to work around the problem is above.

Update (8.31.2011)

MSDN has been updated thanks to our talented technical writer contractor Nicole McAllister, so check out the official post that confirms the above solution to the problem:

http://msdn.microsoft.com/en-us/library/hh394044(v=VS.92).aspx

Nicole didn’t just update MSDN, but it was her idea that led to the solution originally! So once again huge thanks for Nicole for helping me backfill MSDN for various missing pieces of documentation.


TWiT v1.8 Released to Marketplace

SplashScreenImage

I am once again happy to report that the TWiT app for Windows Phone 7 has been updated and is now available in the Marketplace in v1.8.

(v1.7 was also released one day before v1.8 therefore this blog post covers the  release notes for both versions)

The goal of these rapid release is to focus on bug fixes, performance improvements and overall application stability. After perhaps one or two more release I will be shifting my focus almost exclusively to the Mango (Windows Phone 7.5) version of the app to be released later this fall (4th quarter, 2011)

v1.7 & v1.8 Changes/Fixes include:

  • During audio/video playback I have added back an improved version of the slider for seeking to a specific position with the podcast (by popular demand)
  • Some people were experiencing problems playing video versions of TWiT shows which should now be mostly resolved (see note below for some more detail)
  • Included some performance improvements that should make certain parts of the application “snappier” and allow for quicker resume after tombstoning
  • Added new TWiT network show “Ham Nation” and a few other tweaks/fixes to the show data
  • Removed the last Panorama tab “More” and instead added a Panorama-wide “…” menu with most of the same options
  • Added a “Rate this App” link in the “…” Panorama Menu (Please rate this app and support us!)
  • Removed “Featured Shows” feature as it was not used often (based on user feedback)
  • Live playback now works on app resume from tombstoning (possibly broken in v1.6)
  • New splash screen graphic (as displayed above)
  • Various other bug fixes in various parts of the app including the media player
  • Fixed the spelling of “Calendar”

Video playback notes:

As stated above video playback has had some bug fixes but there might still be issues caused by things such as:

  • Problems with your phones connection to the Internet. Video playback requires a more stable and stronger connection then audio which tends to work even during a weak connection to the Internet
  • New TWiT shows often get published with the audio content first and the video isn’t available right away, so you cannot play the video and will get an error if you try. During those times playing audio will likely work
  • Live video feed is still not available to me in the platform

Technical Notes #WP7DEV:

  • Behind the scenes im also happy to be moving the application to AgFx caching framework for all data loading/caching. This is an EXCELLENT framework for Windows Phone 7 and makes working in a tombstoning oriented data model much easier
  • I have updated to the latest version of the Microsoft Ad Control as they released various bug fixes and improvements
  • I am using the Mango version of the developer tools to makes changes to this app in 7.0 mode, which is working great