在我的 C# Windows Phone 8 应用程序中,我有一个 AppBar。我在这个 AppBar 上有两个图标,一个新图标和一个编辑图标。我想将编辑图标更改为每次按下时的后退图标,然后在再次按下时返回编辑图标。我已经尝试过这段代码,但我得到了一个 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";
}
Xml代码:
<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>
备用 AppBar 代码:
//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);