我可以导航到Windows Phone 7中另一个xaml页面的枢轴项吗?

时间:2012-04-12 10:11:13

标签: c# silverlight windows-phone-7 xaml pivot

我尝试使用此代码导航到另一个页面中的pivotitem,但它仍然不起作用

private void Nada1_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/Nada.xaml?PivotNada.SelectedIndex=0", UriKind.Relative));
    }

任何人都可以帮助我吗?

之前谢谢

5 个答案:

答案 0 :(得分:2)

我已经描述了如何在这里轻松完成(http://wp7pivottest.codeplex.com处的示例项目) http://invokeit.wordpress.com/2012/04/01/navigate-to-selected-pivot-item-wpdev-wp7dev/

public enum PivotDef
{
   One,
   Two,
   Three,
   Four,
}

public static PivotDef SelectedPivot;

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
   switch (SelectedPivot)
   {
      case PivotDef.One:
         this.pvtControl.SelectedItem = this.pvt1;
         break;

      case PivotDef.Two:
         this.pvtControl.SelectedItem = this.pvt2;
         break;

      case PivotDef.Three:
         this.pvtControl.SelectedItem = this.pvt3;
         break;

      case PivotDef.Four:
         this.pvtControl.SelectedItem = this.pvt4;
         break;
   }

   base.OnNavigatedTo(e);
}

答案 1 :(得分:2)

这是一个适合您的解决方案。只需将以下代码添加到目标页面:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    if (NavigationContext.QueryString.ContainsKey("PivotNada.SelectedIndex"))
    {
        int selectedIndex = -1;
        if(int.TryParse(NavigationContext.QueryString["PivotNada.SelectedIndex"].ToString(), out selectedIndex))
        {
            if(selectedIndex != -1)
            {
                pivot.SelectedIndex = selectedIndex;
            }
        }
    }
}

答案 2 :(得分:2)

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
  string strItemIndex;
  if (NavigationContext.QueryString.TryGetValue("goto", out strItemIndex))
  {
    myPivot0.SelectedIndex = Convert.ToInt32(strItemIndex);
  }
  base.OnNavigatedTo(e);
}

请注意,myPivot0是您的数据透视表的名称(根据您的数据透视名称进行更改)。然后,导航:

NavigationService.Navigate(new Uri("/ContactP.xaml?goto=0", UriKind.RelativeOrAbsolute));

其中ContactP.xaml包含枢轴。

答案 3 :(得分:0)

将该索引值作为查询字符串传递,然后更新onNNavigatedTo函数中的pivot.selectedindex值

答案 4 :(得分:0)

您应该能够在OnNavigatedTo方法中设置枢轴的选定索引。也请查看http://christian-helle.blogspot.co.uk/2011/02/working-around-pivot-selectedindex.html