使用SplitPage在LoadState中的错误行为

时间:2012-06-27 15:39:09

标签: c# windows-8 microsoft-metro

我正在使用Windows 8 Metro Newsreader-App(使用C#和XAML)。我在Grouped Items Page(模板)上显示了Feed项。单击会将用户转发到详细信息页面,我将其实现为Split Page。因此,我有一个图像库,用户可以从这个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。
  5. 我知道我改变了顺序(由模板提出)并且我添加了if (this.itemsViewSource.View != null) this.itemsViewSource.View.MoveCurrentTo(item);我可能最好删除。

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

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

1 个答案:

答案 0 :(得分: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..
}