如何导航到Windows Phone中的某个Pivot页面?

时间:2014-01-09 09:25:21

标签: windows-phone-7 windows-phone-8 pivotviewer

我创建了一个包含两个链接的MainPage。两者都会将用户带到新的Pivot页面。 但是,第一个链接将打开Pivot的第一页,而第二个链接将打开Pivot的第二页。

到目前为止,我有以下代码:

的MainPage:

NavigationService.Navigate(new Uri("/PivotTester.xaml?goto=" + i, UriKind.Relative));

然后在PivotTester页面上:

namespace CelticNow
{
public partial class PivotTester : PhoneApplicationPage
{
    PivotTester pivot = new PivotTester();

    public PivotTester()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string strItemIndex;
        if (NavigationContext.QueryString.TryGetValue("goto", out strItemIndex))
            pivot.SelectedIndex = Convert.ToInt32(strItemIndex);

        base.OnNavigatedTo(e);
    }
}
}

我添加了“Pivot pivot = new ...”,因为使用PivotTester.SelectedIndex不起作用。

任何人都可以提供一个解决方案,让我如何使这项工作?感谢。

2 个答案:

答案 0 :(得分:2)

这将对您有所帮助,从代码中删除以下代码行

//Remove if not necessary
protected override void OnNavigatedTo(NavigationEventArgs e)
 {
   string strItemIndex;
    if(NavigationContext.QueryString.Contains("goto"))
    {
      strItemIndex=NavigationContext.QueryString["goto"].ToString();
      pivotControl.SelectedIndex = Convert.ToInt32(strItemIndex);
    }

   base.OnNavigatedTo(e);
  }

修改

在xaml中进行更改

 <Grid x:Name="LayoutRoot" Background="Transparent">
            <!--Pivot Control-->
            <controls:Pivot Title="MY APPLICATION" x:Name="pivotControl">
                <!--Pivot item one-->
                <controls:PivotItem Header="one">
                    <Grid/>
                </controls:PivotItem>
                <!--Pivot item two-->
                <controls:PivotItem Header="two">
                    <Grid/>
                </controls:PivotItem>
            </controls:Pivot>
        </Grid>

答案 1 :(得分:0)

我使用此代码:pivot.SelectedItem = x:Name of the pivot item。工作得很好。