我有一个数据绑定xml阅读器。 MainPage连接到DetailsPage:当用户点击主页面中的名称时,她在DetailsPage中获取该名称的详细信息(年龄,性别,出生日期等)。
我遇到了墓碑式的问题。当我点击windows按钮,然后点击后退按钮我进入一个空的DetailsPage。
解决此问题的最简单方法是什么?我试图使用TombstoneHelper,但它也显示空页。
DetailsPage.xaml
<controls:PanoramaItem Header="" Margin="0,0,0,80">
<ScrollViewer>
<StackPanel>
<TextBlock TextWrapping="Wrap" Width="432" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="0,0,0,10" d:LayoutOverrides="Width" Foreground="#DEFFFFFF"><Run Text="Personal D"/><Run Text="e"/><Run Text="tails"/></TextBlock>
<StackPanel HorizontalAlignment="Left" Width="432" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Margin="20,0,7,0" Foreground="#DEFFFFFF"><Run Text="Name"/><Run Text=":"/></TextBlock>
<TextBlock x:Name="username" TextWrapping="Wrap" Text="{Binding Name}" Foreground="#DEFFFFFF" />
</StackPanel>
<StackPanel HorizontalAlignment="Left" Width="432" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Margin="20,0,7,0" Foreground="#DEFFFFFF"><Run Text="Age"/><Run Text=":"/></TextBlock>
<TextBlock x:Name="age" TextWrapping="Wrap" Text="{Binding Age}" Foreground="#DEFFFFFF"/>
</StackPanel>
<StackPanel HorizontalAlignment="Left" Width="432" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Margin="20,0,7,0" Foreground="#DEFFFFFF"><Run Text="Nationality"/><Run Text=":"/></TextBlock>
<TextBlock x:Name="country" TextWrapping="Wrap" Text="{Binding Country}" Foreground="#DEFFFFFF"/>
</StackPanel>
<StackPanel HorizontalAlignment="Left" Width="432" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Margin="20,0,7,0" Foreground="#DEFFFFFF"><Run Text="Country of Birth"/><Run Text=":"/></TextBlock>
<TextBlock x:Name="cobirth" TextWrapping="Wrap" Text="{Binding Cobirth}" Foreground="#DEFFFFFF"/>
</StackPanel>
<StackPanel HorizontalAlignment="Left" Width="432" Orientation="Horizontal">
<TextBlock TextWrapping="Wrap" Margin="20,0,7,0" Foreground="#DEFFFFFF"><Run Text="Place of Birth"/><Run Text=":"/></TextBlock>
<TextBlock x:Name="fobirth" TextWrapping="Wrap" Text="{Binding Pobirth}" Foreground="#DEFFFFFF"/>
</StackPanel>
</StackPanel>
</ScrollViewer>
</controls:PanoramaItem>
DetailsPage.cs
使用TombstoneHelper;
public User()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait;
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
this.SaveState(); // <- first line
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.RestoreState(); // <- second line
}
Mainpage.Cs
private void UserListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
NavigationService.Navigate(new Uri("/DetailsPage.xaml", UriKind.Relative));
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = (RosterItem)e.AddedItems[0];
((ListBox)sender).SelectedIndex = -1;
}
}
答案 0 :(得分:1)
所有事情都是平等的Tombstone助手应该才能正常工作。请记住,您需要为Tombstone帮助程序命名控件(使用x:Name属性)才能访问它们。还要确保在NavigatedFrom()方法中调用SaveState(),在NavigatedTo()中调用RestoreState()(而不是相反)。
如果这不是代码中其他地方必定的错误。如果您发布代码的相关部分,我们可能会提供帮助。