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.