Windows phone 7 Pivot控件动态添加Pivot项。选择Pivot索引时未处理异常
我在pivot控件中添加pivot项时遇到问题..我想在添加next按钮时在pivot(Mycontrol)中添加pivot项。next按钮的代码Windows phone 7 Pivot控件动态添加Pivot项。选择Pivot索引时未处理异常,windows-phone-7,Windows Phone 7,我在pivot控件中添加pivot项时遇到问题..我想在添加next按钮时在pivot(Mycontrol)中添加pivot项。next按钮的代码 static int selectitem; private void nextbuttonOnclick(object sender, RoutedEventArgs e) { if (selectitem != list.Count() - 1)
static int selectitem;
private void nextbuttonOnclick(object sender, RoutedEventArgs e)
{
if (selectitem != list.Count() - 1)
select(selectitem + 1);
else
{
select(0);
}
}
现在,它调用select方法向pivot添加pivot项
void select(int i)
{
MyControl.Items.Clear();
pivotItem = new PivotItem();
Grid sta = new Grid();
WebBrowser wb = new WebBrowser();
sta.Background = new SolidColorBrush(Colors.White);
var address = "<h3>" + list.ElementAt(i).header + "</h3>" + "<br>" + "<img width=\"949\" height=\"449\" src=" + list.ElementAt(i).Imagee + " >" + "<br>" + list.ElementAt(i).Detail.ToString();
var ByteData = Encoding.UTF8.GetBytes(address);
System.Text.Encoding enc = System.Text.Encoding.UTF8;
string myString = enc.GetString(ByteData, 0, ByteData.Length);
try
{
wb.Loaded += (sendr, ev) =>
{
wb.NavigateToString(myString);
};
}
catch (Exception ex)
{
}
wb.Margin = new Thickness(0, 0, 0, 0);
sta.Children.Add(wb);
pivotItem.Content = sta;
MyControl.Items.Add(pivotItem);
try
{
if (i == -1)
MyControl.SelectedIndex = 0;
else
{
selectitem = i;
MyControl.SelectedIndex = i;
}
}
catch (IndexOutOfRangeException v)
{
}
}
void选择(int i)
{
MyControl.Items.Clear();
数据透视项=新数据透视项();
网格sta=新网格();
WebBrowser wb=新的WebBrowser();
sta.Background=新的SolidColorBrush(Colors.White);
var address=“”+list.ElementAt(i).header++“++”
“++”
“+list.ElementAt(i).Detail.ToString();
var ByteData=Encoding.UTF8.GetBytes(地址);
System.Text.Encoding enc=System.Text.Encoding.UTF8;
string myString=enc.GetString(ByteData,0,ByteData.Length);
尝试
{
wb.已加载+=(发送,ev)=>
{
wb.NavigateToString(myString);
};
}
捕获(例外情况除外)
{
}
wb.余量=新厚度(0,0,0,0);
sta.Children.Add(wb);
Content=sta;
MyControl.Items.Add(数据透视项);
尝试
{
如果(i==-1)
MyControl.SelectedIndex=0;
其他的
{
选择项目=i;
MyControl.SelectedIndex=i;
}
}
捕获(IndexOutOfRangeException v)
{
}
}
但在添加一个异常后,它会显示一个未处理的异常
…thanx提前发生ArgumentException是因为这一行:
MyControl.Items.Clear();
该行删除pivot控件中的所有项,但您的内部selectitem变量没有考虑到这一点。在代码中,始终只有一个透视项(这有点违背透视控件的目的)。当您要添加项目#2时,selectitem变量设置为1,行MyControl.SelectedIndex=1代码>对异常进行案例处理,因为只有1个透视项(基于0的数组,以及所有这些内容)
尝试删除清除上面轴心项目的行,看看它是否适合您。否则,您将不得不更改MyControl.SelectedIndex=i代码>行将MyControl.SelectedIndex=0代码>
仅供参考-当我运行您的示例时,我需要将selecitem初始化为-1,而不是0,否则我会收到相同的错误消息。我还添加了将PivotItem.Header属性设置为某个值的代码,以便更容易在pivot项之间切换
出于好奇,您为什么不通过数据绑定而不是所有这些代码来手动加载项目呢