0

我正在开发一个 Windows 8 Metro Newsreader-App(使用 C# 和 XAML)。Grouped Items Page我在(模板)上显示提要项目。单击将用户转发到详细信息页面,我将其实现为Split Page. 因此,我有一个 Image-Gallery,用户可以从该 DetailPage 导航(并返回)。这工作正常。在 ItemDetailPage 我必须在LoadState函数中分配数据。该模板为我提供了以下解决方案:

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
    // TODO: Assign a bindable group to this.DefaultViewModel["Group"]
    // TODO: Assign a collection of bindable items to this.DefaultViewModel["Items"]

    if (pageState == null)
    {
        // When this is a new page, select the first item automatically unless logical page
        // navigation is being used (see the logical page navigation #region below.)
        if (!this.UsingLogicalPageNavigation() && this.itemsViewSource.View != null)
        {
            this.itemsViewSource.View.MoveCurrentToFirst();
        }
    }
    else
    {
        // Restore the previously saved state associated with this page
        if (pageState.ContainsKey("SelectedItem") && this.itemsViewSource.View != null)
        {
            // TODO: Invoke this.itemsViewSource.View.MoveCurrentTo() with the selected
            //       item as specified by the value of pageState["SelectedItem"]
        }
    }
}

我所做的是以下内容:

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
    if (pageState == null)
    {
        // When this is a new page, select the first item automatically unless logical page
        // navigation is being used (see the logical page navigation #region below.)
        if (!this.UsingLogicalPageNavigation() && this.itemsViewSource.View != null)
        {
            this.itemsViewSource.View.MoveCurrentToFirst();
        }
    }
    else
    {
        // Restore the previously saved state associated with this page
        if (pageState.ContainsKey("SelectedItem") && this.itemsViewSource.View != null)
        {
        this.itemsViewSource.View.MoveCurrentTo(pageState["SelectedItem"]);
        }
    }

    var item = ArticleDataSource.GetItem((int)navigationParameter);

    if (item != null)
    {
        this.DefaultViewModel["Group"] = item.Group;
        this.DefaultViewModel["Items"] = item.Group.Items;
        if (this.itemsViewSource.View != null) this.itemsViewSource.View.MoveCurrentTo(item); // remove?

        // Register this page as a share source.
        this.dataTransferManager = DataTransferManager.GetForCurrentView();
        this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
    }
}
  1. 如果我从 OverviewPage 导航到 DetailsPage,则会显示所选项目 (A)。
  2. 我选择了另一个项目(从列表中),并显示了正确的详细信息 (B)。
  3. 如果我从 DetailsPage 导航到 GalleryPage,则会显示正确项目 (B) 的图像。
  4. 如果我现在返回(到 DetailsPage)不是最后选择的项目(B),而是显示我选择的项目(A)进入 DetailsPage。

我知道我更改了订单(由模板建议)并且我补充if (this.itemsViewSource.View != null) this.itemsViewSource.View.MoveCurrentTo(item);说我可能最好删除。

我认为问题(在步骤 4 中描述)this.itemsViewSource.View是空的,因此(逻辑上)this.itemsViewSource.View.MoveCurrentTo(pageState["SelectedItem"])没有被执行。不幸的是,我无法找出原因或是否是错误。

非常感谢任何帮助或指向教程的链接(可以解决我的问题)!谢谢。

4

1 回答 1

2

关键是根据前一页面状态的需要覆盖navigationParameter。然后item加载并选择。尝试使用

// Override the navigationParameter if a page state is set:
if (pageState != null && pageState.ContainsKey("SelectedItem"))
{
    navigationParameter = pageState["SelectedItem"];
}

var item = ArticleDataSource.GetItem((int)navigationParameter);

if (item != null)
{
    DefaultViewModel["Group"] = item.Group;
    DefaultViewModel["Items"] = item.Group.Items;

    if (itemsViewSource.View != null)
    {
        itemsViewSource.View.MoveCurrentTo(item);
    }
    else
    {
        // A serious error happened here..
    }
}
else
{
    // Oooops, an item disappeared..
}
于 2012-07-03T11:59:29.283 回答