简体   繁体   English

枢轴应用程序Windows Phone 8.1导航

[英]pivot application windows phone 8.1 navigation

I am developing a windows phone application for my firm. 我正在为我的公司开发Windows Phone应用程序。

I've just manually created pivot items in a basic page. 我刚刚在基本页面中手动创建了数据透视项。 The application is working fine except the back button navigation. 除后退按钮导航外,该应用程序运行正常。 When I click on any button inside a pivot item it is navigating to desired page but when i click back button it is always navigating to pivot item 1 instead of the one from which the click event is originated. 当我单击枢纽项目内的任何按钮时,它将导航至所需页面,但是当我单击后退按钮时,它将始终导航至枢纽项目1,而不是起源于click事件的枢纽。

My XAML looks like this 我的XAML看起来像这样

<Grid x:Name="ContentRoot"><Pivot x:Name="Test" Title="test book" FontSize="{ThemeResource TextStyleExtraLargeFontSize}">
        <!--Pivot item one-->
        <PivotItem x:Name="pitem1" Header="item1">
     <Grid>
                <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5*" />
                        <RowDefinition Height="5*" />
                        <RowDefinition Height="5*" />
                    </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>                      
                    <ColumnDefinition Width="10*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="10*"/>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="10*" />
                </Grid.ColumnDefinitions>
                <Button x:Name="bt1" Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="1" Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" Click="bt1_Click"/>               
                <Button x:Name="bt2" Grid.Row="2" Grid.RowSpan="2" Grid.Column="2" Grid.ColumnSpan="1" Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="bt2_Click"/>             
                <Button x:Name="bt3" Grid.Row="3" Grid.RowSpan="2" Grid.Column="4" Grid.ColumnSpan="1" Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="bt3_Click"/>          
                <Rectangle StrokeThickness="0" Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" Grid.ColumnSpan="11" Stroke="#FF015C1F">            
                </Rectangle>
            </Grid>
        </PivotItem>
        <!--Pivot item one-->
        <PivotItem x:Name="pitem2" Header="item2" >             
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="5*" />
                        <RowDefinition Height="5*" />
                        <RowDefinition Height="5*" />
                        <RowDefinition Height="5*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="10*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="10*" />
                    </Grid.ColumnDefinitions>
                    <Button x:Name="bt4" Grid.Row="1" Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="bt4_Click"/ >
                <Button x:Name="bt5" Grid.Row="1" Grid.Column="2" Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="bt5_click" />
                </Grid>            
        </PivotItem>
        <!--Pivot item two-->
        <PivotItem x:Name="pitem3" Header="item3">             
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="10*" />
                    <RowDefinition Height="10*" />
                    <RowDefinition Height="10*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="10*" />
                </Grid.ColumnDefinitions>
                <Button x:Name="bt6" Grid.Row="1"  Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="bt6_Click"/>             
               <Button x:Name="bt7" Grid.Row="1" Grid.Column="2" Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="bt7_Click"/>              
                <Button x:Name="bt8" Grid.Row="2"  Content="" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="bt8_Click" />
              </Grid>
        </PivotItem> 

For example : If I click "bt7" present inside the pivot item 3, it navigates to page and when i click back button it is navigating to pivot item 1 instead of item 3. 例如:如果我单击枢纽项目3内的“ bt7”,它将导航到页面,而当我单击“后退”按钮时,它将导航至枢纽项目1而非项目3。

Am I missing something or the problem is i have created pivot item manually? 我是否缺少某些东西,或者问题是我手动创建了透视项目?

I'm new to C# and XAML. 我是C#和XAML的新手。 Any help would be highly appreciated. 任何帮助将不胜感激。

UPDATE : 更新:

    public home()
    {
        this.InitializeComponent();         
       this.NavigationCacheMode = NavigationCacheMode.Required;
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
    }
    public NavigationHelper NavigationHelper
    {
        get { return this.navigationHelper; }
    }
    public ObservableDictionary DefaultViewModel
    {
        get { return this.defaultViewModel; }
    }
    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
    }
   private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
    {
    }
    #region NavigationHelper registration
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {   
        this.navigationHelper.OnNavigatedTo(e);
    }
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        this.navigationHelper.OnNavigatedFrom(e);         
    }
    #endregion
           private async void bt1_Click(object sender, RoutedEventArgs e)
    {
            home obj = new home();
            var result = await obj.GetToken4(sid); 
          if (result.Substring(0, 2) == "No")
            {                    
                MessageDialog msgbox = new MessageDialog("No records found", "e-Book!");
                await msgbox.ShowAsync();                 
            }
            else
            {                  
                this.Frame.Navigate(typeof(firstpage), result);
            }
        }     
    }
    private async Task<string> GetToken4(string sid)
    {
        string postData = "id=" + sid;
        var res = await post4("parameter", postData);
        return res;
    }
    private async Task<string> post4(string url, string postdata)
    {
        var request = WebRequest.Create(new Uri("myurl")) as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/json";
        byte[] data = Encoding.UTF8.GetBytes(postdata);
        //request.ContentLength = data.Length;
        using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
        {
            await requestStream.WriteAsync(data, 0, data.Length);
        }
        WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
        var responseStream = responseObject.GetResponseStream();
        var sr = new StreamReader(responseStream);
        string received = await sr.ReadToEndAsync();
        return received;
    }
    private async void bt2_Click(object sender, RoutedEventArgs e)
    {
       home obj = new home();
            var result = await obj.GetToken4(sid); 
          if (result.Substring(0, 2) == "No")
            {                    
                MessageDialog msgbox = new MessageDialog("No records found", "e-Book!");
                await msgbox.ShowAsync();                 
            }
            else
            {                  
                this.Frame.Navigate(typeof(secondpage), result);
            }
        }          
    }
    private async Task<string> GetToken5(string sid)
    {
        string postData = "dep=" + sid;
        var res = await post5("parameter1", postData);
        return res;
    }
    private async Task<string> post5(string url, string postdata)
    {
        var request = WebRequest.Create(new Uri("myurl")) as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/json";
        byte[] data = Encoding.UTF8.GetBytes(postdata);
        //request.ContentLength = data.Length;
        using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
        {
            await requestStream.WriteAsync(data, 0, data.Length);
        }
        WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
        var responseStream = responseObject.GetResponseStream();
        var sr = new StreamReader(responseStream);
        string received = await sr.ReadToEndAsync();
        return received;
    }

} }

Your page is completely reloaded when navigating back and therefor all controls will be in their default state (eg pivot is on the first item). 向后导航时,页面将完全重新加载,因此所有控件都将处于默认状态(例如,枢轴位于第一项上)。

You can either save/restore the state in the OnNavigatedTo and OnNavigatedFrom methods, or activate page caching by setting your page's NavigationCacheMode to Enabled or Required in the constructor. 您可以在OnNavigatedTo和OnNavigatedFrom方法中保存/恢复状态,也可以通过在构造函数中将页面的NavigationCacheMode设置为Enabled或Required来激活页面缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM