简体   繁体   English

循环遍历数据透视项并检查标题 wp8

[英]Loop through pivot items and check headers wp8

I have a pivot called "infra" I want to loop through all pivot items in infra and check the header value of each item in order to determine which page I should load for the user.我有一个名为“infra”的数据透视表,我想遍历所有数据透视表项目并检查每个项目的标题值,以确定我应该为用户加载哪个页面。 My code below doesn't seem to work.我下面的代码似乎不起作用。

protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        string headerName;
        if (NavigationContext.QueryString.TryGetValue("goto", out headerName))
        {

            foreach (PivotItem pi in infra.Items)
            {
                if (pi.Header.ToString() == headerName)
                    infra.SelectedItem = pi;
            }

        }
        base.OnNavigatedTo(e);
    }

Any ideas on how I can do this a variation on the code above worked fine for a panorama but I had to change to a pivot.关于如何做到这一点的任何想法,上面代码的变体都适用于全景图,但我不得不更改为枢轴。

Additional Info: I am dynamically creating buttons based on some JSON on a panorama page, I am also dynamically creating pivot items based on some other JSON.附加信息:我在全景页面上基于一些 JSON 动态创建按钮,我也在基于其他一些 JSON 动态创建数据透视项。 The buttons should take the user to a specific Pivot Item.这些按钮应将用户带到特定的枢轴项。 A buttons "x:name" attribute is the same as a pivot items header.按钮“x:name”属性与数据透视项标题相同。

I'm not sure why you are trying to compare index (name of a variable strItemIndex ) to Header .我不确定您为什么要尝试将 index (变量strItemIndex名称)与Header You can easily switch PivotItem by using SelecedIndex:您可以使用 SelecedIndex 轻松切换 PivotItem:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    string strItemIndex;
    if (NavigationContext.QueryString.TryGetValue("goto", out strItemIndex))
        infra.SelectedIndex = int.Parse(strItemIndex);
    base.OnNavigatedTo(e);
}

EDIT - after comments and OP's edit:编辑- 在评论和 OP 编辑​​之后:

If you need to recognize your PivotItem with Header, then your code looks ok.如果您需要使用 Header 识别您的 PivotItem,那么您的代码看起来没问题。 It can be optimalized a little:可以稍微优化一下:

string headerName;
PivotItem itemToNavigate = null;
if (NavigationContext.QueryString.TryGetValue("goto", out headerName))
    itemToNavigate = infra.Items.FirstOrDefault(x => (x as PivotItem).Header.ToString() == headerName) as PivotItem;
if (itemToNavigate != null) infra.SelectedItem = item;
else infra.SelectedIndex = 0; // navigate to default one (remember to check first if there are any items)

Where can be problems:哪里可能出问题:

  • check if your infra is filled with items when the method is being invoked,在调用方法时检查您的infra是否充满了项目,
  • check how headers look like - do they contain a right string after ToString() ,检查标题的外观 - 它们在ToString()之后是否包含正确的字符串,
  • check if headerName pulled out from query string is all right.检查从查询字符串中提取的headerName是否正常。

All the three steps you should be able to check via debugging.您应该能够通过调试检查所有三个步骤。

The way Romasz suggested you is correct way, but you can also use below to solve your problem. Romasz 建议您的方式是正确的,但您也可以使用下面的方法来解决您的问题。

protected override void OnNavigatedTo(NavigationEventArgs e)
    {

        string headerName;
        if (NavigationContext.QueryString.TryGetValue("goto", out headerName))
        {

            for (var i = 0; i < infra.Items.Count; i++)
            {
                if (((PivotItem)infra.Items[i]).Header == headerName)
                {
                    infra.SelectedIndex = i;
                    break;
                }
            }

        }
        base.OnNavigatedTo(e);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM