I am working on a Windows 8 Metro Newsreader-App (with C# and XAML). I show the feed-items on a Grouped Items Page (template). A click forwards the user to a detail-page, which I implemented as a Split Page. Therefore, I have an Image-Gallery where the user can navigate from this DetailPage (and back). This works fine. On the ItemDetailPage I have to assign the Data in the LoadState function. The template offers me the following solution:
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"]
}
}
}
What I did was the following:
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);
}
}
- If I navigate from the OverviewPage to the DetailsPage the selected item (A) is shown.
- I select an other item (from the list) and the correct details (B) are shown.
- If I navigate from the DetailsPage to the GalleryPage the images of the correct item (B) are shown.
- If I now navigate back (to the DetailsPage) not the last selected item (B) but the item I selected (A) to enter DetailsPage is shown.
I am aware of the fact that I changed the order (proposed by the template) and I added if (this.itemsViewSource.View != null) this.itemsViewSource.View.MoveCurrentTo(item); that I’d probably better remove.
I think that the problem (described in step 4) is, that this.itemsViewSource.View is null and therefore (logically) this.itemsViewSource.View.MoveCurrentTo(pageState["SelectedItem"]) doesn’t get executed. Unfortunately, I was unable to find out why or if this is the bug.
Any help or link to a tutorial (which could solve my problem) are really much appreciated! thanks.
The point is to override the navigationParameter as needed by the previous page state. The
itemis then loaded and selected. Try using