简体   繁体   English

MVVM和Windows Phone Hub控制

[英]MVVM and the Windows Phone Hub Control

I have asked some questions about MMVM here recently and have appreciated the responses. 我最近在这里问了一些有关MMVM的问题,并感谢您的回答。 I have a further question about filtered views of the same underlying data source and how that should work. 关于同一基础数据源的筛选视图及其应如何工作,我还有一个问题。

Let's say I have a simple list of "items" and want to show various filtered views of this, all in a Hub control. 假设我有一个简单的“项目”列表,并希望在Hub控件中显示各种过滤视图。 Each view would be exposed by a separate HubSection. 每个视图将通过单独的HubSection公开。 So, I might have a HubSection that shows all items, another that shows items due today, another for items this week and a final one for this month. 因此,我可能有一个HubSection显示所有项目,另一个显示今天到期的项目,另一个显示本周的项目,最后一个显示本月的项目。

. All . 全部。 Today . 今天。 This Week . 本星期 。 This Month 这个月

My plan is to bind each HubSection to a separate property of the viewmodel (let's keep this simple and assume the datacontext for the hub is set and each HubSection binds to a property of that datacontext). 我的计划是将每个HubSection绑定到视图模型的单独属性(让我们保持简单,并假设已设置中心的数据上下文,并且每个HubSection都绑定到该数据上下文的属性)。 So, properties of type ObservableCollection called, say, GetAlltems(), GetTodaysItems(), GetThisWeeksItems() and GetThisMonthsItems(). 因此,ObservableCollection类型的属性称为GetAlltems(),GetTodaysItems(),GetThisWeeksItems()和GetThisMonthsItems()。

My question is about how and when I fetch the data from the model. 我的问题是关于如何以及何时从模型中获取数据。 One option would be to declare and populate four ObservableCollection properties when the viewmodel is created (in it's constructor?). 一种选择是在创建视图模型时(在其构造函数中)声明并填充四个ObservableCollection属性。 It seems that the problem there is that I have four copies of essentially the same data in memory (just with different filters applied), as well as the same data being in the data store itself (SQLite in my case). 看来问题在于我在内存中有四个基本相同的数据副本(只是应用了不同的过滤器),并且相同的数据存在于数据存储本身中(在我的例子中是SQLite)。

Another option would be to NOT populate these properties when the viewmodel is created but instead just bind to properties that fetch the appropriate filted dataset and return it as an ObservableCollection on the fly. 另一种选择是在创建视图模型时不填充这些属性,而仅绑定到获取适当的过滤数据集并即时将其作为ObservableCollection返回的属性。 But I am unsure what sort of performance that would have, as the user swipes across the HubSections. 但是我不确定当用户在HubSections上滑动时会产生什么样的性能。 If a HubSection is "offscreen" (say the 4th HubSection, while the first one is onscreen), is the bound list control populated at that point or does that only happen when it comes into view? 如果HubSection在屏幕外(例如第四个HubSection,而第一个在屏幕上),那么绑定列表控件是在此时填充的还是仅当它出现时才发生?

The datasets for each of the filtered views is small (the "all" collection might be a couple of hundred, max). 每个过滤后的视图的数据集都很小(“所有”集合的最大数量可能是数百个)。 But on a mobile device it still seems unnecessary to pre-populate the four properties upfront. 但是在移动设备上,似乎仍不需要预先预先填充这四个属性。

Anyway, this seems like a reasonable common requirements (multiple HubSection controls, each bound to a filtered set of the same underlying data source). 无论如何,这似乎是一个合理的通用要求(多个HubSection控件,每个控件都绑定到相同基础数据源的一组过滤后的控件)。 So, interested in the most elegant way to do this, while having reasonable performance and memory usage. 因此,对最优雅的方式感兴趣,同时具有合理的性能和内存使用率。

I haven't even got to changes to the collections yet and how notifications might work :) 我什至还没有更改集合以及通知可能如何工作:)

Thanks. 谢谢。

Take a look at CollectionViewSource it allows to use the same set of objects but define filters. 看一下CollectionViewSource它允许使用相同的对象集,但定义过滤器。 I do have to say that the implementation has changed a bit with winrt wp8.1. 我确实必须说,winrt wp8.1的实现已发生了一些变化。 So double check the documentation on how to actually set the filter. 因此,请仔细检查有关如何实际设置过滤器的文档。 I think the way to go these days is by using simple Linq queries 我认为这些天的方法是使用简单的Linq查询

The MSDN doc MSDN文档

http://msdn.microsoft.com/library/windows/apps/br209833 http://msdn.microsoft.com/library/windows/apps/br209833

From some older sources ( for wp8 SL ) 从一些较旧的来源(适用于wp8 SL)

http://www.hanselman.com/blog/CollectionViewSourceIsCrazyUsefulForBindingToFilteredObservableCollectionsOnWindowsPhone8.aspx http://www.spikie.be/blog/post/2012/04/12/Filtering-collections-from-XAML-using-CollectionViewSource.aspx http://www.hanselman.com/blog/CollectionViewSourceIsCrazyUsefulForBindingToFilteredObservableCollectionsOnWindowsPhone8.aspx http://www.spikie.be/blog/post/2012/04/12/Filtering-collections-from-XAML-using-CollectionViewSource.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM