如何在 Windows Phone 8 中的特定索引处插入 PivotItem?

How to insert PivotItem at particular index in Windows Phone 8?

我目前正在使用 Pivot Control,我想根据从服务器获取的列表在特定索引处插入 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);
            }

我得到的异常是指定的参数超出了有效值的范围。参数名称索引

此外,索引的顺序可能不同,例如,我可能需要根据 mw_header_list 项目索引在任何索引处插入上述项目。

请有人建议我怎样才能达到我的要求?

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

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

if (sr_index>MWPivot.Items.Count)
{
    MWPivot.Items.Add(service_requestPivotItem);
} else {
    MWPivot.Items.Insert(sr_index, service_requestPivotItem);
}

我终于实现了我想做的事情。感谢@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();
        }

希望对大家有所帮助。我刚刚使用 foreach 循环添加了 Pivot dynamic。