ApplicationBarIconButton 为空 [英] ApplicationBarIconButton is null

查看:24
本文介绍了ApplicationBarIconButton 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的 ApplicationBarIconButton 为 null?

Why is my ApplicationBarIconButton null?

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" x:Name="appBar">
        <shell:ApplicationBarIconButton x:Name="appbarSave"
          IconUri="/Icons/appbar.save.rest.png Text="Save" IsEnabled="False"
          Click="appbarSave_Click" />
    </shell:Application Bar>
</phone:PhoneApplicationPage.ApplicationBar>

appBarSave 对象为空,尝试这个:

The appBarSave object is null, and trying this:

Initialize Component();
appbarSave.IsEnabled = true;

导致 NullReferenceException.该对象唯一起作用的地方是单击事件(如果我启用它):

Results in a NullReferenceException. The only place the object works is in the click event (if I enable it):

private void appbarSave_Click(object sender, EventArgs e)
{
    ApplicationBarIconButton button = (ApplicationBarIconButton)sender;
    button.IsEnabled = false;
}

我真的希望能够将保存按钮启动为禁用状态并在稍后启用它.

I would really like to be able to start the save button as disabled and enabled it later.

推荐答案

我记得之前遇到过这个问题:有一个解释 此处.一个简单的解决方法是在代码隐藏而不是 xaml 中实例化它(例如 此处).

I remember running into this issue before: there's an explanation here. An easy workaround is just to instantiate it in code-behind rather than xaml (like here).

private ApplicationBarIconButton SaveEdit;
private void InitAppBar()
{
     ApplicationBar appBar = new ApplicationBar();

     SaveEdit = new ApplicationBarIconButton(new Uri("images/appbar.check.rest.png", UriKind.Relative));
     SaveEdit.Click += new EventHandler(OnClick_Check);
     SaveEdit.Text = Strings.Save_button;
     appBar.Buttons.Add(SaveEdit);

     ApplicationBarIconButton CancelEdit = new ApplicationBarIconButton(new Uri("images/appbar.close.rest.png", UriKind.Relative));
     CancelEdit.Click += new EventHandler(OnClick_Cancel);
     CancelEdit.Text = Strings.Cancel_button;
     appBar.Buttons.Add(CancelEdit);

     ApplicationBar = appBar;
}

这篇关于ApplicationBarIconButton 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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