I am developing a windows phone application for my firm.
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.
My XAML looks like this
<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.
Am I missing something or the problem is i have created pivot item manually?
I'm new to C# and 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;
}
} }