问题
In my C# Windows Phone 8 app, I have an AppBar. I have two icons on this AppBar, one new icon, and one edit icon. I want to change the edit icon, to the back icon whenever it is pressed, and then back to the edit icon whenever it is pressed again. I have tried this code, but I get a nullReferenceException:
public static Uri addIcon = new Uri("/Assets/AppBar/new.png", UriKind.Relative);
public static Uri backIcon = new Uri("/Assets/AppBar/back.png", UriKind.Relative);
//Edit the projects
if (editMode.Equals(false))
{
//EditMode is off, enable edit mode and modify the editprojectMenuButton
editMode = true;
editprojectMenuButton.IconUri = backIcon;
editprojectMenuButton.Text = "finish";
} else
{
//EditMode is on, disable edit mode and modify the editprojectMenubutton
editMode = false;
editprojectMenuButton.IconUri = addIcon;
editprojectMenuButton.Text = "edit";
}
Xaml code:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton x:Name="editprojectMenuButton" IconUri="/Assets/AppBar/edit.png" Text="Edit" Click="editprojectMenuButton_Click"/>
<shell:ApplicationBarIconButton x:Name="addprojectMenuButton" IconUri="/Assets/AppBar/new.png" Text="Add" Click="addprojectMenuButton_Click"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem x:Name="aboutButton" Text="About" Click="aboutButton_Click"/>
<shell:ApplicationBarMenuItem x:Name="howtoButton" Text="How To" Click="howtoButton_Click"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
Alternate AppBar Code:
//Create the AppBar
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.IsMenuEnabled = true;
addprojectMenuBtn = new ApplicationBarIconButton(new Uri("BarIcons/add.png", UriKind.Relative));
addprojectMenuBtn.Text = "add";
addprojectMenuBtn.Click += addprojectMenuButton_Click;
editprojectMenuBtn = new ApplicationBarIconButton(new Uri("BarIcons/edit.png", UriKind.Relative));
editprojectMenuBtn.Text = "add";
editprojectMenuBtn.Click += editprojectMenuButton_Click;
ApplicationBar.Buttons.Add(addprojectMenuBtn);
ApplicationBar.Buttons.Add(editprojectMenuBtn);
回答1:
It seems that when you create AppBar in xaml, your reference to Button in code behind is null.
I'm making my AppBar programatically and I've not spotted such problem:
ApplicationBar = new ApplicationBar();
ApplicationBar.Mode = ApplicationBarMode.Default;
ApplicationBar.IsMenuEnabled = false;
ApplicationBarIconButton addBtn = new ApplicationBarIconButton(new Uri("BarIcons/add.png", UriKind.Relative));
addBtn.Text = "Add";
addBtn.Click += addBtn_Click;
ApplicationBarIconButton infoBtn = new ApplicationBarIconButton(new Uri("BarIcons/info.png", UriKind.Relative));
infoBtn.Text = "Info";
infoBtn.Click += infoBtn_Click;
ApplicationBar.Buttons.Add(addBtn);
ApplicationBar.Buttons.Add(infoBtn);
You can also add menu items there and what you want. Then if you ApplicationBarIconButton is 'global' variable, you can change it any time, and it works.
EDIT - some explanations
Here I've found similar problem, and at this blog is explanation.
There is also a code which works:
Microsoft.Phone.Shell.ApplicationBarIconButton btn = ApplicationBar.Buttons[0] as Microsoft.Phone.Shell.ApplicationBarIconButton;
btn.IconUri = backIcon;
btn.Text = "My button";
来源:https://stackoverflow.com/questions/20293376/programmatically-changing-the-appbar-icons