以编程方式更改应用栏图标 [英] Programmatically changing the appbar icons

查看:234
本文介绍了以编程方式更改应用栏图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#Windows Phone 8应用程序中,我有一个AppBar.我在此AppBar上有两个图标,一个是新图标,一个是编辑图标.我想将编辑图标更改为在每次按下时都返回到后退图标,然后在每次再次按下时都返回到编辑图标.我已经尝试了这段代码,但是得到了nullReferenceException:

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代码:

    <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代码:

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);

推荐答案

似乎在xaml中创建AppBar时,后面代码中对Button的引用为空. 我正在以编程方式制作我的AppBar,但没有发现这样的问题:

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);

您还可以在其中添加菜单项以及所需的菜单项.然后,如果ApplicationBarIconButton是'global'变量,则可以随时更改它,并且它可以工作.

编辑-一些说明
此处我发现了类似的问题,并且

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";

这篇关于以编程方式更改应用栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆