【问题标题】:Got an exception when adding application bar programatically以编程方式添加应用程序栏时出现异常
【发布时间】:2012-12-25 19:28:17
【问题描述】:

在我的应用程序中,我想在运行时添加应用程序栏。我有一个包含应用程序栏名称和图像 uri 的列表。我必须根据列表添加应用程序栏。但我得到一个例外“指定参数超出了有效值的范围”。任何人都可以给我一个解决方案吗?下面是我的代码。

public void createObjectsForApplicationbar(List<AppBarDetails> appbarList)
    {
        int i = 0;
        foreach (Others menus in appbarList)
        {                                                          
          UpdateAppbarButton(i, menus.menu_image, menus.name, true, ApplicationBarIconButton_Click);
          i++;
        }
        ShowButtons(menuNames1);
    }                                                                                 



private void UpdateAppbarButton(int index, string uriString, string text, bool visibility, EventHandler handler)
    {
        ApplicationBarIconButton button1 = null;
        this.ApplicationBar = new ApplicationBar();
        this.ApplicationBar.IsVisible = true;
        this.ApplicationBar.Opacity = 1;
        this.ApplicationBar.IsMenuEnabled = true;
        if (this.ApplicationBar.Buttons.Count > index)
        {
            button1 = this.ApplicationBar.Buttons[index] as ApplicationBarIconButton;
        }

        if (button1 != null)
        {
            {
                this.ApplicationBar.Buttons.Remove(button1);
            }
        }
        if (visibility == true)
        {
            button1 = new ApplicationBarIconButton(new Uri(uriString, UriKind.RelativeOrAbsolute));
            button1.Text = text;
            button1.Click += handler;
            this.ApplicationBar.Buttons.Insert(index, button1);// here i got the exception "Specified argument was out of the range of valid values when the value of  i=1"
        }
    }

【问题讨论】:

  • 在哪一行抛出了异常?
  • @ColinE this.ApplicationBar.Buttons.Insert(index, button1);
  • 当i=1时出现异常
  • 重点是List.Insert不能将值插入索引,小于零大于List.Count。
  • 我已经删除了两个 if 条件我得到了相同的异常,即 if(this.ApplicationBar.Buttons.Count > index) { button1 = this.ApplicationBar.Buttons[index] as ApplicationBarIconButton; } if (button1 != null) { { this.ApplicationBar.Buttons.Remove(button1); } }

标签: c# windows-phone-7 windows-phone-7.1 windows-phone-8 application-bar


【解决方案1】:

你应该重新考虑条件的逻辑。

if (this.ApplicationBar.Buttons.Count > index)
            {
                button1 = this.ApplicationBar.Buttons[index] as ApplicationBarIconButton;

                this.ApplicationBar.Buttons.Remove(button1);

                if (visibility == true)
                {
                    button1 = new ApplicationBarIconButton(new Uri(uriString, UriKind.RelativeOrAbsolute));
                    button1.Text = text;
                    button1.Click += handler;
                    this.ApplicationBar.Buttons.Insert(index, button1);
                }
            }

            else
            // insert it anyway?
            {
                if (visibility == true)
                {
                    button1 = new ApplicationBarIconButton(new Uri(uriString, UriKind.RelativeOrAbsolute));
                    button1.Text = text;
                    button1.Click += handler;
                    this.ApplicationBar.Buttons.Add(button1);
                }
            }

不确定,你真正想要的是什么,但没有例外。

【讨论】:

  • 谢谢 Olter,但即使列表中有 8 个项目,我也只有一个应用程序栏作为输出
  • 该死,我什至没有意识到,你每次都在创建一个新的ApplicationBar,你调用这个事件......你为什么给它起一个名字“更新”?看起来更像是单一方法中的“创建和更新”。你最好把它们分成两个独立的方法,在构造函数中调用“Create”,当你真的想更新它时调用“Update”。
猜你喜欢
  • 2019-12-19
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多