Cannot clear the icon while in a list - ERROR

May 20, 2013 at 10:48 PM
Hello. I am having this nasty error "Cannot clear the icon while in a list" message.


and if I do this,
Path cannot be absolute? :/

my icons are set to Content and Copy if newer

this is my application bar xaml
May 22, 2013 at 2:41 PM
Hi there,

For the first error, this is expected, because you haven't set the IconUri for app bar icon button. If you programmably set IconUri of an app bar icon button that has been added to an app bar in code-behind, you'll get this error in runtime.

For the second, this error also occurs when you set an absolute uri to IconUri. I also encounter this issue after switching to SDK 8.0. I guess this is because the XAML designer mistakenly convert the path string to a URI with UriKind.Absolute. But in runtime, everything just goes well.

So why these runtime errors occur in design time? I think that's because the XAML designer instantiates those objects behind the scene, just like you put an MVVM view model as a resource in XAML and bind it to control, and you'll have the data shown up in the designer.

Knowing the cause definitely helps resolve these issues, and it's simple to get rid of these errors. Detect whether it's in design mode. If so, don't add the buttons to app bar. Specifically, add the following code to HandleButtonsPropertyChanged in AppBar.cs before any other code in the method body:
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
Again, these issues don't block your app from running correctly. I'll apply the fix in the next release. But in case you don't want to see these errors shown up in your Visual Studio/Blend at this point of time, go ahead and fix it by yourself :)

Sep 17, 2014 at 2:44 AM
As Allen said both issues are really little annoyances than actual errors. However they can make waste your time as you will be spending few seconds each time you open a page in Blend or VS IDE. I have a version of the AppBar inspired by this project and other similar ones, and have same problem. I fix both using small special handling in the setter and constructor.

For the first error "Cannot clear the icon while in a list"
 public BindableAppBarIconButton()
            Button = new ApplicationBarIconButton ();
            Button.Text = "Text";
            //FIx for the "Cannot clear the icon while in a list" 
            Button.IconUri = new Uri ("/Content/ApplicationBar.Add.png", UriKind.Relative);
For the Path cannot be absolute issue
public Uri IconUri
            get { return Button.IconUri; }
                if ( !value.IsAbsoluteUri )
                    Button.IconUri = value;
                    //Fix for the designed creating an Absolute Uri from the relative path string 
                    Button.IconUri = new Uri (value.ToString ().Split ('/').Last (), UriKind.Relative);
But what Allen suggested might be easy and clean fix.