windows-phone-7windows-phone-8pivotviewer

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


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.


Solution

  • 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>