How to disable Windows 8/WinRT AppBar?

jokeefe picture jokeefe · Aug 14, 2012 · Viewed 7.4k times · Source

My goal is to only have an AppBar available under a certain circumstance. I am attempting to accomplish this by creating an AppBar, but leaving it disabled until that circumstance arises. However, if you set the IsEnabled attribute on an AppBar to False, when you launch your app and right-click (which typically opens the AppBar), the application crashes. Is this a bug in the framework? What is the proper way to disable an AppBar?

EDIT: It also occurs when you set Visibility to Collapsed.

More info: I am running it through the Visual Studio debugger, but a separate "Visual Studio Just-In-Time Debugger" window is popping up with the message "An unhandled win32 exception occurred in App.exe [2596]." A warning box pops up on top of that that says "A debugger is attached to App.exe but not configured to debug this unhandled exception. To debug this exception, detach the current debugger."

EDIT 2: It's not just my code. It also crashes if you just add IsEnabled="False" to the AppBar in Microsoft's own sample AppBarControl project (found here: http://code.msdn.microsoft.com/windowsapps/XAML-AppBar-control-sample-2aa1cbb4)

EDIT 3: @G. Andrew Duthie - devhammer provided the answer I am using. I just wanted to add that I found that it is best to use this.BottomAppBar = null to disable it as opposed to setting the IsEnabled or Visibility properties. If you just set Visibility to Collapsed, then when you right-click, the app still thinks an AppBar is present even though it isn't visible, so your next regular click will be interpreted as the click that typically dismisses the AppBar, so you'll have to click a second time to actually carry out the action you were attempting.

Answer

devhammer picture devhammer · Aug 14, 2012

I've been playing with this for a bit (using both IsEnabled, as well as setting the Visibility property to Visibility.Collapsed), and the only way I could successfully disable the AppBar without raising the exception was to show the AppBar first. Once the AppBar has been shown, setting IsEnabled to false, or setting Visibility to Visibility.Collapsed no longer throws the exception.

However, if you create the AppBar programmatically, like so:

myAppBar = new AppBar();
StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Horizontal;
Button myButton = new Button();
myButton.Content = "Click Me";
sp.Children.Add(myButton);
myAppBar.Content = sp;

but don't add it to Page.BottomAppBar until the first time you need it, you won't get the exception.

I tested using a button with the following handler:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    if (this.BottomAppBar == null)
    {
        this.BottomAppBar = myAppBar;
    }
    else {
        this.BottomAppBar = null;
    }
}

When the app loads the first time, if you attempt to invoke the AppBar, nothing happens. Click the button (which adds the AppBar to Page.BottomAppBar), and invoking the AppBar will show the AppBar. Click the button again, and the AppBar is no longer shown (though myAppBar is still instantiated and ready when you need it again).

Hope that helps!