简体   繁体   中英

NullReferenceException Error When Trying to Change the Property IsEnabled of an AppBar Item

On one of my .xaml pages, I have an appbar with a few icons on it.

One of the icons pins the page to Start, so when it is pinned I want to change the IsEnabled property of that icon to false.

However I get this weird error; as described in the title when this procedure is called.

Here's the code:

if (Tile == null) { }
else { appBarPin.IsEnabled = false; }

any ideas?

The behavior with the Application Bar is different to the rest of UI elements. From App bar for Windows Phone :

The app bar doesn't support some common features of controls, such as data-binding. As a result, you can't change the icon button and menu item text by using Name properties that you set in XAML.

If you want to change a property of the appbar item, do it the following way:

ApplicationBarIconButton button = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
button.IsEnabbled = false;

Replace the 0 with the index of the button. Ie if the button is the second button of the appbar, the index will be 1.

See more in How to change app bar icon buttons and menu items dynamically for Windows Phone

A null reference exception means that you can't say ".IsEnabled" if the thing before the dot is null.

It appears that appBarPin is null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM