【问题标题】:WP7 Tombstoning - Easy practiceWP7 Tombstoning - 简单练习
【发布时间】:2011-07-16 22:23:50
【问题描述】:

我有一个数据绑定的 xml 阅读器。 MainPage 连接到 DetailsPage:当用户点击 Main Page 中的姓名时,她会在 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;

        }
    }

【问题讨论】:

    标签: windows-phone-7 tombstoning


    【解决方案1】:

    所有事情都是平等的墓碑助手应该正常工作。请记住,您需要为 Tombstone 助手命名控件(使用 x:Name 属性)才能访问它们。还要确保在 NavigatedFrom() 方法中调用 SaveState() 并在 NavigatedTo() 中调用 RestoreState()(而不是相反)。

    如果这不是代码中的其他地方一定有问题。如果您发布代码的相关部分,我们可能会提供帮助。

    【讨论】:

    • 我使用 Name 属性,但它仍然不起作用。我添加了 ...From(e);this.SaveState(); ... ...到(e); this.RestoreState()... 仅将代码添加到 DetailsPage,我是否也必须将它们添加到 MainPage?该问题的另一种可能性是;我在 Scrollviewer 中有详细信息,它会影响结果吗?
    • TombstoneHelper 默认只管理 TextBoxes、PasswordBoxes、CheckBoxes、RadioButtons、Sliders、ListBoxes 和 ScrollViewers 的内容。它不起作用,因为您正在向 TextBlocks 显示值,而 TombstoneHelper 不会自动处理这些值。您可以查看 TombstoneHelper 的可扩展性模型,了解如何添加对 TextBlocks 的支持,否则您可以在 TextBoxes 中显示数据。
    • 艾登,我不知道该怎么感谢你:D。现在可以了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多