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

标签 c# .net xaml data-binding windows-8

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

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

Link

我想要的很简单 - 一些自定义 ItemDetailPages。我希望能够单击 GroupDetailPageGroupedItemsPage 上的一些项目并导航到自定义 .xaml 文件,其中一个我可以包含不止一张图片。

我确信有一种我错过的简单方法,而且我也确信这些信息对很多人都有用,所以我将悬赏这个问题.

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

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

 /// <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; }
}

对于不同的数据类型是不可能的。那么在这种情况下我能做什么呢?

非常感谢。

最佳答案

I have a simple grid application; how do I make it possible to have one of the elements in the group item page link to a custom item detail page ?

好的,让我们来看看使用 Visual Studio 中的“网格应用程序”模板时生成的应用程序。

组项目页面上元素的数据类是 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 上有类似的行为,您只需以相同的方式更新那里的点击事件处理程序。

希望对您有所帮助。

关于c# - Windows 8 网格应用程序中的自定义详细信息页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435892/

相关文章:

c# - StreamReader Read 方法不读取指定的字符数

c# - 如何从 TimeSpan 中删除秒数?

c# - 无法使用 asp.net mvc 3 获得事件模糊

xaml - 使用ScrollViewer进行图像缩放时,可以保持向左平移

xaml - 在 VisualStateManager (WinRT XAML) 中更改 ItemTemplate 中控件的属性

c# - 这个 Font 对象什么时候被释放?

c# - 两个 DateTimes 之间的时差?

c# - 是否有 CPU 模拟器或一种方法来查看事物是如何在内存中创建和销毁的

c# - 从应用程序资源加载 .NET 程序集并从内存中运行它,但不终止主/主机应用程序

xaml - 如何在UWP中设置RatingControl星星的颜色?