10

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

如果你不熟悉这个布局,这里有一个简短的解释:

关联

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

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

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

CustomDataItemSampleDataSource.cs课堂上创建了一个:

 /// <summary>
    /// Generic item data model.
    /// </summary>
    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<SampleDataGroup> _allGroups = new ObservableCollection<SampleDataGroup>();
public ObservableCollection<SampleDataGroup> AllGroups
{
    get { return this._allGroups; }
}

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

非常感谢。

4

2 回答 2

3

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

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

于 2013-03-16T17:15:23.187 回答
3

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

好的,让我们来看看使用 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 事件处理程序。

希望有帮助。

于 2013-03-19T06:56:59.403 回答