【发布时间】: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