C# 如何在Windows Phone 8中的特定索引处插入数据透视项?

C# 如何在Windows Phone 8中的特定索引处插入数据透视项?,c#,windows-phone-8,C#,Windows Phone 8,我目前正在使用Pivot控件,我想根据从服务器获取的列表在特定索引处插入PivotItem。当我尝试下面的代码时,会得到一个异常 MWPivot.Items.Clear(); if(MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST !=null) { List<SUBPARAM> mw_header_list = new List<SUBPARAM>();

我目前正在使用Pivot控件,我想根据从服务器获取的列表在特定索引处插入PivotItem。当我尝试下面的代码时,会得到一个异常

MWPivot.Items.Clear();
        if(MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST !=null)
        {
            List<SUBPARAM> mw_header_list = new List<SUBPARAM>();
            mw_header_list = MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST.SUB_PARAMS;
            if(mw_header_list.Any(header=>header.SP_CODE.Equals("SERVICE_REQUEST")))
            {
                SUBPARAM SR_SUB_PARAM = mw_header_list.FirstOrDefault(item => item.SP_CODE.Equals("SERVICE_REQUEST"));
                PivotItem service_requestPivotItem = new PivotItem();
                service_requestPivotItem.Header = SR_SUB_PARAM.SP_TITLE;
                Grid SR_Grid = new Grid();
                SR_Grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                SR_Grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

                ReportsSearchBox = new PhoneTextBox();
                ReportsSearchBox.Hint = "Search Reports";
                ReportsSearchBox.ActionIcon = new BitmapImage(new Uri("/Images/search.png", UriKind.RelativeOrAbsolute));
                ReportsSearchBox.TextChanged += SearchReports;

                Grid.SetRow(ReportsSearchBox, 0);
                SR_Grid.Children.Add(ReportsSearchBox);

                reportsListBox = new ListBox();
                reportsListBox.Margin = new Thickness(10, 0, 10, 0);
                reportsListBox.HorizontalAlignment = HorizontalAlignment.Center;
                reportsListBox.ItemContainerStyle = App.Current.Resources["GenericListBoxContainerStyle"] as Style;
                reportsListBox.ItemTemplate = this.Resources["MWReportsTemplate"] as DataTemplate;
                reportsListBox.SelectionChanged += reportsListBox_SelectionChanged;

                Grid.SetRow(reportsListBox, 1);
                SR_Grid.Children.Add(reportsListBox);

                service_requestPivotItem.Content = SR_Grid;
                var sr_index = mw_header_list.FindIndex(item => item.SP_CODE.Equals("SERVICE_REQUEST"));
//Get Exception on below line 
                MWPivot.Items.Insert(sr_index, service_requestPivotItem);
            }
MWPivot.Items.Clear();
if(MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST!=null)
{
List mw_header_List=新列表();
mw_header_list=MCSManager.Instance.mwmmenuitemslist.mw_header_list.SUB_参数;
if(mw_header_list.Any(header=>header.SP_CODE.Equals(“服务请求”))
{
子参数SR_SUB_PARAM=mw_header_list.FirstOrDefault(item=>item.SP_CODE.Equals(“服务请求”);
数据透视项服务_requestPivotItem=新数据透视项();
service_requestPivotItem.Header=SR_SUB_PARAM.SP_TITLE;
网格SR_Grid=新网格();
SR_Grid.RowDefinitions.Add(new RowDefinition(){Height=new GridLength(1,GridUnitType.Auto)});
SR_Grid.RowDefinitions.Add(new RowDefinition(){Height=new GridLength(1,GridUnitType.Star)});
ReportsSearchBox=新的PhoneTextBox();
ReportsSearchBox.Hint=“搜索报告”;
ReportsSearchBox.ActionIcon=新的位图图像(新Uri(“/Images/search.png”,UriKind.RelativeOrAbsolute));
ReportsSearchBox.TextChanged+=搜索报告;
Grid.SetRow(ReportsSearchBox,0);
SR_Grid.Children.Add(报告搜索框);
reportsListBox=新列表框();
reportsListBox.Margin=新厚度(10,0,10,0);
reportsListBox.HorizontalAlignment=HorizontalAlignment.Center;
reportsListBox.ItemContainerStyle=App.Current.Resources[“GenericListBoxContainerStyle”]作为样式;
reportsListBox.ItemTemplate=this.Resources[“MWReportsTemplate”]作为数据模板;
reportsListBox.SelectionChanged+=reportsListBox\u SelectionChanged;
Grid.SetRow(报告列表框,1);
SR_Grid.Children.Add(报表列表框);
service_requestPivotItem.Content=SR_网格;
var sr_index=mw_header_list.FindIndex(item=>item.SP_CODE.Equals(“服务请求”);
//在下面的行中获取异常
MWPivot.Items.Insert(sr_索引,service_请求数据透视项);
}
我得到的异常是指定的参数超出了有效值的范围。参数名称索引

此外,索引顺序可能不同,例如,我可能需要在基于mw_header_列表的任何索引中插入上述项目


请有人建议我如何达到我的要求?

看起来您试图将项目添加到不存在的位置。例如,如果您有一个包含3个项目的集合,则只能将新项目添加到位置0、1、2、3,而不是4或更高

因此,在代码中添加一个检查来处理这个问题,并进行相应的处理(如果索引太高,则在末尾添加项目,或者其他)


看起来您正在尝试将项目添加到不存在的位置。例如,如果您有一个包含3个项目的集合,则只能将新项目添加到位置0、1、2、3,而不能添加到位置4或更高的位置

因此,在代码中添加一个检查来处理这个问题,并进行相应的处理(如果索引太高,则在末尾添加项目,或者其他)


最后,我终于实现了我想做的事情。Thanks@IgorKulman谢谢你激励我

请在下面找到我的代码

 MWPivot.Items.Clear();
        MCSProgressIndicator.ActivateProgressIndicator(MCSManager.Instance.currentTextParams.COMMON_PLEASE_WAIT);
        if (MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST !=null)
        {
            mw_header_list = new List<SUBPARAM>();
            mw_header_list = MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST.SUB_PARAMS;
            foreach(var header in mw_header_list)
            {
                PivotItem mwPivotItem = new PivotItem()
                {
                    Name=header.SP_CODE,
                    Header=header.SP_TITLE
                };
                createViewForPivotItem(mwPivotItem, header.SP_CODE);
                MWPivot.Items.Add(mwPivotItem);
                //MWPivot.SelectionChanged += MWPivot_SelectionChanged;
            }
            if (userSettings.Contains("SelectedIndex"))
                MWPivot.SelectedIndex = Int32.Parse(userSettings["SelectedIndex"].ToString());
            MCSProgressIndicator.deactivateProgressIndicator();
        }
MWPivot.Items.Clear();
MCSProgressIndicator.ActivateProgressIndicator(MCSManager.Instance.currentTextParams.COMMON_请稍候);
if(MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST!=null)
{
mw_标题_列表=新列表();
mw_header_list=MCSManager.Instance.mwmmenuitemslist.mw_header_list.SUB_参数;
foreach(mw_标题列表中的var标题)
{
PivotItem mwPivotItem=新的PivotItem()
{
名称=header.SP_代码,
Header=Header.SP_TITLE
};
createViewForPivotItem(mwPivotItem,header.SP_代码);
MWPivot.Items.Add(mwPivotItem);
//MWPivot.SelectionChanged+=MWPivot\u SelectionChanged;
}
if(userSettings.Contains(“SelectedIndex”))
MWPivot.SelectedIndex=Int32.Parse(userSettings[“SelectedIndex”].ToString());
MCSProgressIndicator.禁用ProgressIndicator();
}

希望这对其他人有所帮助。我刚刚通过使用foreach循环添加了Pivot dynamic。

最后,我实现了我想要做的事情。Thanks@IgorKulman谢谢你激励我

请在下面找到我的代码

 MWPivot.Items.Clear();
        MCSProgressIndicator.ActivateProgressIndicator(MCSManager.Instance.currentTextParams.COMMON_PLEASE_WAIT);
        if (MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST !=null)
        {
            mw_header_list = new List<SUBPARAM>();
            mw_header_list = MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST.SUB_PARAMS;
            foreach(var header in mw_header_list)
            {
                PivotItem mwPivotItem = new PivotItem()
                {
                    Name=header.SP_CODE,
                    Header=header.SP_TITLE
                };
                createViewForPivotItem(mwPivotItem, header.SP_CODE);
                MWPivot.Items.Add(mwPivotItem);
                //MWPivot.SelectionChanged += MWPivot_SelectionChanged;
            }
            if (userSettings.Contains("SelectedIndex"))
                MWPivot.SelectedIndex = Int32.Parse(userSettings["SelectedIndex"].ToString());
            MCSProgressIndicator.deactivateProgressIndicator();
        }
MWPivot.Items.Clear();
MCSProgressIndicator.ActivateProgressIndicator(MCSManager.Instance.currentTextParams.COMMON_请稍候);
if(MCSManager.Instance.MWMenuItemsList.MW_HEADER_LIST!=null)
{
mw_标题_列表=新列表();
mw_header_list=MCSManager.Instance.mwmmenuitemslist.mw_header_list.SUB_参数;
foreach(mw_标题列表中的var标题)
{
PivotItem mwPivotItem=新的PivotItem()
{
名称=header.SP_代码,
Header=Header.SP_TITLE
};
createViewForPivotItem(mwPivotItem,header.SP_代码);
MWPivot.Items.Add(mwPivotItem);
//MWPivot.SelectionChanged+=MWPivot\u SelectionChanged;
}
if(userSettings.Contains(“SelectedIndex”))
MWPivot.SelectedIndex=Int32.Parse(userSettings[“SelectedIndex”].ToString());
MCSProgressIndicator.禁用ProgressIndicator();
}

希望这对其他人有所帮助。我刚刚通过使用foreach循环添加了Pivot dynamic。

sr_索引和MWPivot.Items.Count的值是多少