简体   繁体   中英

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. 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. The buttons should take the user to a specific Pivot Item. A buttons "x:name" attribute is the same as a pivot items header.

I'm not sure why you are trying to compare index (name of a variable strItemIndex ) to Header . You can easily switch PivotItem by using SelecedIndex:

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:

If you need to recognize your PivotItem with Header, then your code looks ok. 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,
  • check how headers look like - do they contain a right string after ToString() ,
  • check if headerName pulled out from query string is all right.

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.

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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