MVVM-Light定位器模式和可重复使用的UserControl [英] MVVM-Light Locator Pattern and Reusable UserControl

查看:174
本文介绍了MVVM-Light定位器模式和可重复使用的UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Silverlight 4应用程序,正在尝试将其转换为使用定位器模式和MVVM-Light库.

I have an existing Silverlight 4 application that I am attempting to convert to use the locator pattern and the MVVM-Light library.

我的理解是MvvmLocator类保留对每个视图模型的引用,并允许您将每个视图的DataContext绑定到定位器中的适当ViewModel.

My understanding is that the MvvmLocator class keeps a reference to each view model and allows you to bind the DataContext of each view to an appropriate ViewModel in the locator.

我的应用程序使用导航框架,因此应用程序中的每个页面都是一个UserControl,该控件已加载到内容"框架中.

My application uses a navigation frame and hence each page in the app is a UserControl that gets loaded into the Content frame.

我可以看到将每个页面视图绑定到定位器中的相应ViewModel多么容易,但是我的问题是我有其他具有自己的视图模型的UserControl可以在多个上下文中重复使用,这会导致问题如果控件的每个实例都绑定到相同的最终视图模型实例.

I can see how easy it is to bind each page view to the appropriate ViewModel in the locator, but my problem is that I have other UserControls with their own view models that get re-used in multiple contexts and it would cause problems if each instance of the control got bound to the same ultimate view model instance.

例如,我有一个搜索页面,其中包含2个用户控件.第一个是搜索参数控件,其中包含几个相互依赖的组合框和一个搜索按钮.第二个是结果网格本身.包含这两个控件的页面的视图模型处理它们之间的通信.问题在于,该搜索控件也会在其他页面上使用,而让它们都使用相同的视图模型实例将引起各种麻烦.因此,使用普通的"DataContext = {Binding ProductSearch,Source = {StaticResource Locator}}"似乎是不可能的...

For example, I have a search page that consists of 2 user controls. The first is a search parameter control that contains several inter-dependent combo boxes and a search button. The second is the results grid itself. The view model of the page that contains these 2 controls handles the communication between them. The problem is that this search control gets used on other pages as well, and having them all use the same view model instance would cause all kinds of headaches. So, using the normal "DataContext={Binding ProductSearch, Source={StaticResource Locator}}" seems out of the question...

有其他人遇到这个问题或有什么聪明的主意如何解决吗?我在想,也许我可以修改定位器,以便为使用控件的每个页面创建视图模型的新实例.否则,我只能在每次页面转换时强制执行清除操作,但是那样一来,我将无法保存页面之间的状态.

Has anyone else run into this or have any bright ideas how to solve it? I'm thinking maybe I could modify the locator to create a new instance of the view model for each page where the control is used. Otherwise, I could just force a cleanup on every page transition, but then I wouldn't be able to save state between pages.

我是整个Silverlight和MVVM领域的新手,所以也许我缺少有关所有内容的结构或使自己陷入困境的一些知识.我不确定.我绝对欢迎任何建议.

I am new to the whole Silverlight and MVVM world, so maybe I am missing something about how this all should be structured or have painted myself into a corner. I'm just not sure. I would definitely welcome any suggestions.

推荐答案

您的主要问题是,Silverlight导航框架在导航到视图时始终会创建一个新的视图实例.您可以使用INavigationContentLoader覆盖此行为,并存储要重用的视图实例.

Your main problem is the fact that the Silverlight navigation framework always creates a new instance of a view when you navigate to it. You can override this behavior using INavigationContentLoader and storing instances of your views to be re-used.

使用INavigationContentLoader的一些示例是David Poll的打开Silverlight 4导航:INavigationContentLoader简介或Mike Taultry的

Some examples of using INavigationContentLoader are David Poll's Opening up Silverlight 4 Navigation: Introduction to INavigationContentLoader or Mike Taultry's Silverlight 4 Rough Notes: Taking Control of Navigation blog posts. These posts only show how to create your own content loader; using it to reload existing views is up to you.

这篇关于MVVM-Light定位器模式和可重复使用的UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆