How can I navigate to a certain Pivot page in Windows Phone?

大城市里の小女人 提交于 2019-12-11 18:06:24

问题


I have created a MainPage with two links. Both will take the user to a new Pivot page. However, the first link will open the first page of the Pivot, while the second will open the second page of the Pivot.

I have the following code so far:

MainPage:

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

and then on PivotTester page:

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

I added in the "Pivot pivot = new..." as using PivotTester.SelectedIndex wouldn't work.

Can anyone provide a solution as to how I would make this work? Thanks.


回答1:


This will help you, Remove below line of code from your code

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

EDIT

Make changes in your 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>



回答2:


I use this code: pivot.SelectedItem = x:Name of the pivot item. Working good.



来源:https://stackoverflow.com/questions/21016140/how-can-i-navigate-to-a-certain-pivot-page-in-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!