5

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Erik
  • 799
  • 2
  • 15
  • 27
  • Can you show some more code? I think probably you have your button a local variable, so when you try to change it later (from method for example) you get a NullReference exception. – Romasz Nov 29 '13 at 21:42
  • I dont know if i understood your question right, but I added the appbar in xaml. I've added the xaml code – Erik Nov 29 '13 at 21:45
  • On what you line you get the Exception? – Andy Nov 29 '13 at 21:52
  • The on that changes the image: editprojectMenuButton.IconUri = backIcon; – Erik Nov 29 '13 at 21:52
  • I've the same problem with code you provided, it seems, that editprojectMenuButton is null, even it's set in xaml. When I do this programatically I've no such problems. Thought I cannot say exact why methos from xaml gives null. – Romasz Nov 29 '13 at 22:02
  • That's so strange, why can you then change TextBlocks, buttons, TextBoxes, but not AppBars? – Erik Nov 29 '13 at 22:10

1 Answers1

2

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";
Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • I created the app bar with the code i added above, and placed it inside the public MainPage() method, before the InitializeComponent() method call. but it doesn't show the AppBar – Erik Nov 29 '13 at 22:23
  • Have you tried after InitializeComponent? I've also added a method to get to those created in xaml. – Romasz Nov 29 '13 at 22:27
  • I tried yeah, didn't work. Probably just some tiny typeo or something. Your last code worked perfect, thanks! – Erik Nov 29 '13 at 22:49