Windows 8网格应用程序中的自定义详细信息页

我创建了一个简单的C#Windows 8网格应用程序。

如果您不熟悉此布局,请在此处对其进行简要说明:

链接

我想要的是简单的 – 一些自定义ItemDetailPages 。 我希望能够点击GroupDetailPageGroupedItemsPage上的一些项目并导航到自定义.xaml文件,我可以在其中包含多个图像。

我确信有一个简单的方法,我错过了,我也相信这些信息对很多人都有用,所以我会在这个问题上提供赏金。

到目前为止,我一直在努力做到这一点:

我在SampleDataSource.cs类中创建了一个CustomDataItem

  ///  /// Generic item data model. ///  public class CustomDataItem : SampleDataCommon { public CustomDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content, SampleDataGroup group) : base(uniqueId, title, subtitle, imagePath, description) { this._content = content; this._group = group; } private string _content = string.Empty; public string Content { get { return this._content; } set { this.SetProperty(ref this._content, value); } } private SampleDataGroup _group; public SampleDataGroup Group { get { return this._group; } set { this.SetProperty(ref this._group, value); } } } 

但是,显然,添加到ObservableCollection

 private ObservableCollection _allGroups = new ObservableCollection(); public ObservableCollection AllGroups { get { return this._allGroups; } } 

使用不同的数据类型是不可能的。 那么在这种情况下我该怎么办?

非常感谢。

我有一个简单的网格应用程序; 如何使组项目页面中的某个元素链接到自定义项目详细信息页面成为可能?

好吧,让我们从Visual Studio中获取使用“Grid App”模板时生成的应用程序。

组项目页面上元素的数据类是SampleDataItem类。 您可以做的是添加一些指示如何处理导航的数据字段( boolint或其他)。 在这个例子中,我们保持简单,所以我们添加一个bool来指示导航是否是自定义的。

 public class SampleDataItem : SampleDataCommon { // add flag as last param public SampleDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content, SampleDataGroup group, bool isCustomNav = false) : base(uniqueId, title, subtitle, imagePath, description) { this._content = content; this._group = group; this.IsCustomNav = isCustomNav; } // to keep it simple this doesn't handle INotifyPropertyChange, // as does the rest of the properties in this class. public bool IsCustomNav { get; set; } ... } 

因此,当您要添加要显示的新SampleDataItem对象时,只需在构造函数中设置isCustomNav字段即可。

现在,我们所要做的就是更改已分组项目页面(GroupedItemsPage.xaml.cs)网格中已存在的单击事件处理程序:

 void ItemView_ItemClick(object sender, ItemClickEventArgs e) { // Navigate to the appropriate destination page, configuring the new page // by passing required information as a navigation parameter var item = (SampleDataItem)e.ClickedItem; var itemId = item.UniqueId; if (item.IsCustomNav == false) { // default this.Frame.Navigate(typeof(ItemDetailPage), itemId); } else { // custom page this.Frame.Navigate(typeof(ItemDetailPage2), itemId); } } 

我们上面所做的就是获取所选项目,然后测试我们之前添加的导航标记。 基于此,我们导航到原始的ItemDetailPage或一个名为ItemDetailPage2的新的。 正如我之前提到的,导航标志不一定是bool 。 它可以是intenum或其他类型,告诉我们在哪里导航。

请注意,如果您想在GroupDetailsPage上使用类似的行为,则只需以相同的方式更新click事件处理程序。

希望有所帮助。

是的,您应该能够创建自定义或不同的数据类型。 如果使用网格模板创建Win8应用程序,您会看到该模板为您做了三件事:1)它创建了三种类型,SampleDataCommon,它是基础SampleDataItem,它实现了SampleDataCommon并添加了两个新属性 – 内容和组和SampleDataGroup也实现了SampleDataCommon,添加了一个方法ItemsCollectionChanged,并添加了两个属性Items和TopItems。 2)它创建一个名为SampleDataSource的类,其中创建了一个SampleDataGroup集合并命名为AllGroups:ObservableCollection AllGroups。 3)它将SampleDataSource的Items和AllGroup绑定到XMAL页面中的对象。

在您的情况下,您使用相同的数据结构。 换句话说,您将创建一个包含项目等的组。