MVVM-灯光定位器模式和可重用用户控件

发布于 2024-09-06 15:36:07 字数 766 浏览 1 评论 0原文

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

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

我的应用程序使用导航框架,因此应用程序中的每个页面都是加载到内容框架中的用户控件。

我可以看到将每个页面视图绑定到定位器中适当的 ViewModel 是多么容易,但我的问题是我有其他 UserControls 及其自己的视图模型,这些视图模型可以在多个上下文中重用,如果每个实例控件的绑定到相同的最终视图模型实例。

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

还有其他人遇到过这个问题或者有任何好主意如何解决它吗?我想也许我可以修改定位器来为使用该控件的每个页面创建视图模型的新实例。否则,我可以在每个页面转换时强制进行清理,但随后我将无法保存页面之间的状态。

我对整个 Silverlight 和 MVVM 世界都是新手,所以也许我错过了这一切应该如何构建的一些东西,或者让我自己陷入了困境。我只是不确定。我绝对欢迎任何建议。

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

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.

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

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2024-09-13 15:36:09

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

使用 INavigationContentLoader 的一些示例是 David Poll 的 打开 Silverlight 4 导航:INavigationContentLoader 简介 或 Mike Taultry 的 Silverlight 4 粗略笔记:控制导航 博客文章。这些帖子仅展示如何创建您自己的内容加载器;使用它来重新加载现有视图取决于您。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文