Prism for Silverlight:如何在区域内按特定顺序维护视图
我正在为 SL 创建一种“导航面板”(实际上是一个 ItemControl),并使用区域来允许每个模块将其链接添加到面板。
问题是模块加载不一致,因此面板中的链接顺序可能会根据模块加载顺序而改变。
限制模块顺序是不可能的。
其他可行的选择是对绑定到 ItemControl 的区域视图集合进行排序,问题是 ViewCollection 非常有限,因此对其进行排序非常困难。
我错过了一个选择吗,你有什么想法吗?
谢谢 爱丽儿
I am creating sort of a "Navigation panel" (which is actually an ItemControl) for SL and using Regions to allow each module to add his link to the panel.
Problem is that modules loading is inconsistent and thus order of links in the panel can change according to modules loading order.
Restricting the modules order is out of the question.
Other feasible option is the order the region's Views Collection that is binded to the ItemControl, the problem is that ViewCollection is very limited, so ordering it is pretty hard.
Did I miss an option, do you have an idea?
Thanks
Ariel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Prism4 中,您只需将 ViewSortHintAttribute 应用于您的视图:
区域上的默认排序比较器将选择此属性并对视图进行相应的排序。您可以将任何字符串放入属性中,但我倾向于使用中等大小的数字,这样我可以轻松地在现有视图之间放置新视图。
In Prism4 you just apply the ViewSortHintAttribute to your views:
The default sort comparer on the regions will pick up this attribute and sort the views accordingly. You can put any string into the attribute but I tend to use medium sized numbers that allow me to easily put a new view in between existing ones.
参考山姆的答案,你首先必须建立你的比较器。下面的一个也能够提出没有专门希望定位的视图。要将此比较器附加到必须排序的区域,您可以使用棱镜手册中介绍的方法:
Refering to Sam's answer you first have to build your comparer. The following one is also capable of views that do not have a dedicated wish to be positioned at. To attach this comparer to the region that has to be sorted you can use a way intruduced by the prism manual:
至少在 prism V4 中,您可以告诉区域经理如何对特定区域中的视图进行排序。您只需要提供该区域的比较函数即可。
此示例按一个非常愚蠢的值(函数名称)排序:
当然,在设置 SortComparison 之前,区域管理器需要知道该区域。到目前为止,我发现实现此目的的唯一解决方法是推迟使用调度程序设置比较函数:
当然,您应该使用一些比类名更有用的信息来排序,但这应该很容易解决。
At least in prism V4 there you can tell the region manager how to sort the views in a specific region. You just need to provide a compare function to the region.
This example sorts by a very stupid value, the function name:
Of course the region needs to be known to the region manager before you can set the SortComparison. So far the only workaround I found to achieve this was to defer to set the comparison function using the Dispatcher:
Of course you should use some more useful information than the class name for the sorting order, but this should be easy to solve.
这没有内置到 Prism 区域中,但它很容易实现。
Damian Schenkelman 发布了他创建的一个扩展方法,用于将区域添加到索引中,该方法似乎工作得很好。
http://blogs.southworks.net/dschenkelman/2009/03/14/how-to-add-a-view-to-a-region-in-a-pspecial -index-with-prism-v2/
希望这有帮助。
This is not built into Prism regions, however it's easily implementable.
Damian Schenkelman has posted an extension method he created for adding a region to an index that seems to work pretty well.
http://blogs.southworks.net/dschenkelman/2009/03/14/how-to-add-a-view-to-a-region-in-a-particular-index-with-prism-v2/
Hope this helps.
我发现 Sam 的解决方案有效,但发现它在所有视图都添加到该区域时执行排序,从而对视图进行两次排序。
尽管它仍然是一个有效的解决方案,但阅读 Prism 讨论中的 这篇 帖子让我想到了一种方法仅在加载区域时但在添加任何视图之前执行此操作。
1 - 订阅 Regions 集合的 CollectionChanged
我将其放置在 Shell ViewModel 代码中,该代码与包含我要排序的区域的视图关联。每当 IRegionManager 导入已解决时,我都会订阅其 Regions 集合的 CollectionChanged 事件:
2 - 更改事件委托中区域的 SortComparison
然后委托
Regions_CollectionChanged
将执行每当更新 Regions 集合并将更改我所需区域的SortComparison
时:3 - 定义 CompareNavigatorViews 委托
在我的例子中,我只是按视图的标题对视图进行排序包含它们的程序集,您可以在此处实现您自己的比较方法。请记住,您将在此处收到的对象是视图而不是视图模型。
以防万一有人问,AssemblyInfo 类是我制作的实用程序类。要获取程序集的标题,您可以使用此功能:
希望这对某人有帮助!
I found that Sam's solution worked, but discovered that it executes the sort when all views have been added to the region, thus sorting the views twice.
Although it is still a valid solution, reading this post in Prism discussion made me think about a way of implementing this just when the region has been loaded, but before any views have been added yet.
1 - Subscribe to the CollectionChanged of Regions collection
I placed this in the Shell ViewModel code which is the one associated to the View that contains the region I want to sort. Whenever the IRegionManager import has been resolved I subscribe to the CollectionChanged event of its Regions collection:
2 - Change the SortComparison of the region in the event delegate
Then the delegate
Regions_CollectionChanged
will execute whenever the Regions collection is updated and will change theSortComparison
of my desired region:3 - Define the CompareNavigatorViews delegate
In my case, I just sort the views by the title of the assembly where they are contained, you can implement your own compare method here. Remember that the objects you'll receive here are the Views and not the ViewModels.
Just in case somebody asks, the AssemblyInfo class is an utility class I made. To get the title of an assembly you could use this function:
Hope this helps someone!
以及缺乏答案计数。我还没有找到 Prism 的解决方案。
相反,我使用 MEF 来解决这个问题。
我将就此写一篇博文并更新此占位符。
Well as the lack of answers counting. I have not found a solution with Prism.
Instead I've used MEF to solve this.
I will write a blog post on it and update this place holder.