我正在为我的公司开发一个Windows Phone应用程序。
我只是在基本页面中手动创建了透视项目。除了后退按钮导航,应用程序工作正常。当我点击枢轴项目内的任何按钮时,它将导航到所需的页面,但是当我单击后退按钮时,它始终导航到透视项目1而不是发起点击事件的项目。
我的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>
例如:如果我点击&#34; bt7&#34;在枢轴项目3内部,它导航到页面,当我单击后退按钮时,它将导航到枢轴项目1而不是项目3.
我错过了什么或问题是我手动创建了支点项目吗?
我是C#和XAML的新手。任何帮助都将受到高度赞赏。
更新:
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;
}
} }
答案 0 :(得分:0)
导航回来时,您的页面会完全重新加载,因此所有控件都将处于默认状态(例如,枢轴位于第一个项目上)。
您可以在OnNavigatedTo和OnNavigatedFrom方法中保存/恢复状态,也可以通过在构造函数中将页面的NavigationCacheMode设置为Enabled或Required来激活页面缓存。