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

时间:2015-12-29 13:43:36

标签: c# windows-phone-8

我目前正在使用Pivot Control,我想在基于我从服务器获取的List的特定索引处插入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 项目索引在任何索引处插入上述项目。

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

2 个答案:

答案 0 :(得分:0)

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

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="send_request.php" name="myForm1" method="post" onsubmit="return(validate1());">
  <select class="regtext sec1test" name="select" id="select1" style="width:99%">
    <option value="0">Select What?</option>
    <option value="1">Features</option>
    <option value="2">Video</option>
    <option value="3">Buy1</option>
    <option value="4">Buy2</option>
  </select>
  <div class="sec1 lef">
    <select name="type" class="regtext sec1test" id="select2" style="width:99%">
      <option id="0" value="0">Select genre</option>
      <option value="1" data-id="1">abc</option>
      <option value="2" data-id="2">models</option>
      <option value="3" data-id="3">Comp</option>
      <option value="4" data-id="4">Beat</option>
    </select>
  </div>
  <div class="sec1 lef">
    <select name="type" class="regtext sec1test" id="select3" style="width:99%">
      <option id="0" value="0">Select genre</option>
      <option value="1" data-id="1">abc</option>
      <option value="2" data-id="2">mod</option>
      <option value="3" data-id="3">Comp</option>
      <option value="4" data-id="4">Beat</option>
    </select>
  </div>
</form>

答案 1 :(得分:0)

最后,我能够实现我想做的事情。谢谢@ 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动态。