我创建了一个简单的 C# Windows 8 网格应用程序。
如果你不熟悉这个布局,这里有一个简短的解释:
我想要的很简单 - 一些自定义 ItemDetailPages
。我希望能够单击 GroupDetailPage
和 GroupedItemsPage
上的一些项目并导航到自定义 .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
类。您可以做的是添加某种类型的数据字段(bool
、int
或其他)来指示如何处理导航。在这个例子中,我们保持简单,所以我们添加一个 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
。它可以是 int
或 enum
或其他告诉我们导航位置的类型。
请注意,如果您希望在 GroupDetailsPage
上有类似的行为,您只需以相同的方式更新那里的点击事件处理程序。
希望对您有所帮助。
关于c# - Windows 8 网格应用程序中的自定义详细信息页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435892/