0

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

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

主页:

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 不起作用。

任何人都可以就我将如何完成这项工作提供解决方案吗?谢谢。

4

2 回答 2

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>
于 2014-01-09T09:46:25.667 回答
0

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

于 2014-01-09T09:30:26.193 回答