在Windows 8网格应用程序自定义详细页自定义、网格、应用程序、详细

2023-09-04 02:35:44 作者:大鼻涕嘎嘎

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

如果你不熟悉这个布局,有它的一个简要说明这里:

Link

我想有很简单 - 一些自定义的 ItemDetailPages 。我希望能够点击一些项目上的 GroupDetailPage GroupedItemsPage 并导航到一个自定义的的.xaml 文件,人们在那里我可以包括多个图像。

我敢肯定有这样做,我已经错过了一个简单的方法,而且我也相信这个信息对很多人有用的,所以我将提供关于这个问题的赏金

我一直在努力与这样至今:

我已经创建了一个 CustomDataItem SampleDataSource.cs 类:

  ///<总结>
    ///通用项目的数据模型。
    ///< /总结>
    公共类CustomDataItem:SampleDataCommon
    {
        公共CustomDataItem(字符串UNIQUEID,字符串标题,副标题字符串,字符串的ImagePath,字符串描述,字符串内容,SampleDataGroup组)
            :基地(UNIQUEID,标题,副标题,的ImagePath,说明)
        {
            this._content =内容;
            this._group =组;
        }

        私人字符串_content =的String.Empty;
        公共字符串内容
        {
            {返回this._content; }
            集合{this.SetProperty(REF this._content,价值); }
        }

        私人SampleDataGroup _group;
        公共SampleDataGroup集团
        {
            {返回this._group; }
            集合{this.SetProperty(REF this._group,价值); }
        }
    }
 

不过,很明显,增加了的ObservableCollection

 私人的ObservableCollection< SampleDataGroup> _allGroups =新的ObservableCollection< SampleDataGroup>();
公众的ObservableCollection< SampleDataGroup> AllGroups
{
    {返回this._allGroups; }
}
 
Win8处女秀版本为Build 7985

是不可能用不同的数据类型。所以,我能做些什么在这种情况下?

多谢了。

解决方案   

我有一个简单的网格应用;我该如何使人们有可能在集团网页链接到一个自定义项目详细信息页面中的一个元素?

好吧,让我们正在使用从Visual Studio中的网格应用程序模板时生成的应用程序。

为组项目页面上的元素数据类是 SampleDataItem 类。你可以做的是添加某种类型的数据字段(布尔 INT ,或其他方式)的指示如何处理导航。在这个例子中,我们只使用简单的,所以我们增加一个布尔指示导航是否自定义或没有。

 公共类SampleDataItem:SampleDataCommon
{
    //添加标志作为最后的参数
    公共SampleDataItem(字符串UNIQUEID,标题字符串,字符串副标题
        字符串的ImagePath,字符串描述,字符串内容,SampleDataGroup组,
        布尔isCustomNav = FALSE)
    :基地(UNIQUEID,标题,副标题,的ImagePath,说明)
    {
        this._content =内容;
        this._group =组;
        this.IsCustomNav = isCustomNav;
    }

    //保持它的简单,这并不处理INotifyPropertyChange,
    //一样在这个类中的属性的其余部分。
    公共BOOL IsCustomNav {获得;组; }

    ...
}
 

所以,当您添加要显示一个新的 SampleDataItem 的对象,你只需要设置 isCustomNav 字段在构造函数中。

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

 无效ItemView_ItemClick(对象发件人,ItemClickEventArgs E)
{
    //导航到相应的目标页面,配置了新的一页
    //通过传递所需的信息作为导航参数
    VAR项目=(SampleDataItem)e.ClickedItem;
    VAR的itemId = item.UniqueId;

    如果(item.IsCustomNav ==假)
    {
        // 默认
        this.Frame.Navigate(typeof运算(ItemDetailPage)的itemId);
    }
    其他
    {
        //自定义页面
        this.Frame.Navigate(typeof运算(ItemDetailPage2)的itemId);
    }
}
 

我们要做上面得到所选择的项目,然后测试,我们先前添加的导航标志。在此基础上,我们定位到原始 ItemDetailPage 或新建一个名为 ItemDetailPage2 。正如我前面提到的,导航标志不必须是布尔。它可以是一个 INT 枚举或其它类型,它告诉我们在哪里浏览。

请注意,如果你想对类似行为的 GroupDetailsPage ,你只需要更新的Click事件处理程序也同样如此。

希望有所帮助。

I have created a simple C# Windows 8 grid application.

If you're unfamiliar with this layout, there is a brief explanation of it here :

Link

What I would like to have is simple - some custom ItemDetailPages. I'd like to be able to click on some items on the GroupDetailPage and the GroupedItemsPage and navigate to a custom .xaml file, one where I can include more than one image.

I'm sure there is a simple way of doing that that I have missed out on, and I'm also sure that this information will be useful for a lot of people, so I will be offering a bounty on this question.

I have struggled with doing this so far :

I've created a CustomDataItem in the SampleDataSource.cs class :

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

However, obviously, adding to the ObservableCollection

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

is impossible with a different data type. So what can I do in this case ?

Thanks a lot.

解决方案

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 ?

Ok, lets take the app that is generated when using the "Grid App" template from Visual Studio.

The data class for the elements on the group items page is the SampleDataItem class. What you can do is add some type of data field (bool, int, or other) that indicates how to handle the navigation. In this example, we are keeping it simple, so we add a bool to indicate whether the navigation is custom or not.

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

    ...
}

So when you are adding a new SampleDataItem object to be displayed, you just need to set the isCustomNav field in the constructor.

Now all we have to do is change the already existing click event handler in the grid on the grouped item page (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);
    }
}

All we are doing above is getting the selected item and then testing the navigation flag that we added earlier. Based on this we navigate to either the original ItemDetailPage or a new one called ItemDetailPage2. As I mentioned before, the navigation flag doesn't have to be a bool. It can be an int or enum or some other type that tells us where to navigate.

Note that if you want similar behavior on the GroupDetailsPage, you just have to update the click event handler there the same way.

Hope that helps.