问题标签 [viewmodellocator]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
5961 浏览

wpf - 找不到名为“ViewModelLocator”异常的资源

我试图通过将 ViewModelLocator 声明为 App.xaml 中的资源来使用它。它是一个非常简单的类,如下所示:

App.xaml 文件如下:

App.xaml.cs 如下:

ShellView.xaml 如下:

我可以在 Visual Studio 设计器中看到正确的标题,但是当我运行应用程序时,得到 XamlParseException: 'Provide value on 'System.Windows.StaticResourceExtension' 引发了异常。行号“11”和行位置“9”。

内部异常有{“找不到名为'ViewModelLocator'的资源。资源名称区分大小写。”}

我错过了什么吗?

0 投票
2 回答
1832 浏览

wpf - 使用 viewmodellocator 时将模型传递给 viewmodel

我试图掌握 ViewModelLocator 的概念(在 MVVM Light 中,尽管问题通常适用于 ViewModelLocator 的概念,无论使用哪个 MVVM 框架),我很难弄清楚如何使用它。

据我了解,您的视图使用定位器的单例实例上的属性之一作为它们的数据上下文。定位器定义了这些不同的属性,并为每个属性返回正确的视图模型实例。

这一切都很好,但我很难理解你是如何用视图应该呈现的模型数据实际填充这些视图模型的。

例如,假设我有一个显示员工列表的视图。我可以创建一个EmployeesView 和一个EmployeesViewModel。在 ViewModelLocator 中,我可以创建一个返回此EmployeesViewModel 的属性:

现在,viewmodel 需要一个员工列表,所以我可以创建某种返回所有员工的数据服务,并将其注册到 ViewModelLocator 的构造函数中的 Servicelocator:

因此,这将起作用,如果我实例化EmployeesView,EmployeesViewModel 将被实例化并注入一个返回所有员工的数据服务。

但是,现在我想查看刚刚在EmployeesView 中单击的某个员工的详细信息。该员工大概有某种 ID,可以通过它从数据库或其他任何地方检索他/她。

我可以创建一个 EmployeeDetailsView 和一个 EmployeeDetailsViewModel,并向 ViewModelLocator 添加一个属性:

也许在 ViewModelLocator 的构造函数中注册某种数据服务:

但是我如何告诉数据服务或视图模型他们应该为哪个员工提供详细信息?我在哪里传递员工 ID?

我看这一切都错了吗?有人知道任何好的例子吗?我能找到的所有示例都只返回每个视图模型的相同单个实例。

0 投票
1 回答
302 浏览

silverlight - 这是在 MVVM 中使用 ViewModelLocator 的好方法吗

我们的 Silverlight 应用程序中有一个 ViewModelLocater 类。它由一个基本构造函数和一个用于返回类的 ViewModel 的公共属性组成。代码是这样的

在每个 XAML 页面中,我们使用该页面设置 ViewModel

Setup是上述字典中的键。

Silverlight 应用程序非常大,我们最近才开始调查任何内存泄漏(有很多......)我正在使用 Windbg 来跟踪这些泄漏,我注意到很多内存泄漏导致了 ViewModelLocater 类。每次应用加载 ViewModelLocator 构造函数时,都会为所有视图创建 ViewModel。所以我想知道是否有更好的方法来实现 ViewModelLocator 类。

0 投票
1 回答
1719 浏览

c# - MVVM, ViewModelLocator, dynamically show View with ViewModel based on property

enter image description hereI have a design question. Please look at the attached image to get an idea of the app design. Basicaly I just need help in finding the correct approach here.

I have a View and ViewModel that holds an TileUserControl. It takes a collection of tiles, and displays them in the defined size, color, order and so one. It is also capable of grouping the tales in groups, and allowing the user to add and remove Tiles.

The Tile object look like this.

So far all good. Everything works fine.

All tiles have content. I want to set the content to the View and the ViewModel corresponding to the View string on the Tile Object.

I am using MVVM Light, but may also use Caliburn Mico. The tiles Control is DevExpress TileLayoutControl for WPF.

DevExpress TileLayoutControl doc

EDIT: Updated With MainTilesView XAML

My issue is how to get the UserControl from Tile.View loaded into the Tile content. I have tride above with DataTrigger and DataTemplate.

The StringToTileConverter just Returns the name of the ViewModel.

Currently using Caliburn Micro, and my binding workes fine outside of the template part, but I can't get them to work with the DataTemplate.

Also all this code is not very clean, so therefor I ask if anyone have a more clean way to do it.

0 投票
1 回答
685 浏览

wpf - 如何使用 ViewModelLocator 进行单元测试

我已经使用 Autofac 创建了一个自定义视图模型定位器,并通过 App.xaml 正常设置它,就像通常使用它们中的大多数一样。我的问题是我现在如何进行单元测试?每次我尝试测试初始化​​视图的方法时都会出错

在我的 app.xaml 中:

在每个视图中:

单元测试错误:

我明白为什么在您进行单元测试时,实际上没有实际应用程序的实例,那么解决这个问题的好方法是什么?

ViewModelLocator 代码:

0 投票
1 回答
927 浏览

c# - MVVM Light - ViewModel 重用

我在 Windows Phone 8 中使用 Pivot Control,并且正在使用 MVVM Light。

我创建了一个 UserControl,它将成为每个 PivotItem 的内容,以及一个我的 UserControl 应该使用的 ViewModel。

例如:

通常在我的 UserControl 构造函数中我有这样的东西(我认为这不是一个好习惯?):

此 ViewModel 将根据位置键从数据库中获取数据。我的问题是如何将该密钥传递给 MyViewModel 而不是使 MyViewModel 单例?

0 投票
1 回答
327 浏览

c# - 另一个 MVVMLight 项目中的 MVVMLight

我正在开发一个 MVVMLight / WPF 项目,需要添加一大块功能,其中包括多个视图和视图模型。我知道在不久的将来会在其他项目中使用同样的功能,所以我想把这个功能变成自己的项目,我可以根据需要添加到其他解决方案中,而无需或很少修改。

我首先添加了第二个 MVVMLight 项目(Beta),删除了标准的 MainWindow.xaml 和 MainViewModel.cs 文件,并创建了一个简单的 UserControl 和关联的视图模型。

我可以添加 Beta 作为对原始项目 (Alpha) 的引用,并通过将视图插入堆栈面板来显示视图,如下所示:

执行此操作时,一切似乎都正常工作。我遇到的问题是当我尝试将属性从 TestViewModel 绑定到 TestView 时。

在 TestView 中,如果我这样做:

TestView 在运行时正确显示。但是当我将 TextBlock 绑定到这样的属性时:

该消息未显示,Beta 的定位器似乎被忽略(未绑定数据上下文),并且我从 Snoop 收到以下错误:

我相信这意味着 Test_VM 和 WelcomeMessage 的绑定正试图通过 Alpha Locator 而不是 Beta Locator 找到。我使用的是在每个项目中启动 MVVMLight 项目时默认创建的 ViewModelLocator。

是否有可能拥有第二个“定位器”,如果可以,我需要做什么才能使其工作?

0 投票
0 回答
137 浏览

viewmodel - Viewmodellocater 取消注册 Viewmodels

我已经整天都在寻找解决方案。我是 MVVM 初学者,我有以下问题。这是我的 viewmodelLocator 的代码:

从 A 更新数据后,我想打开 B,我按以下方式进行操作:

这可行,但是 B 使用来自 A 的数据,并且在我关闭程序并再次打开它之前不会显示更新的数据。我已经读过它与 Viewmodellocator 和消息有关,但由于我对此很陌生,所以我现在不知道如何和/或在哪里进行一些“清理”。

谁能帮我解决这个问题?提前致谢。

0 投票
0 回答
2855 浏览

windows-phone-8 - “IXXXX 已注册” - MVVM-Light

我正在使用mvvm-light我的 WP8 应用程序。

我不断收到IXXXXX is already registered错误,如果我使用这个解决方案:http: //developingux.com/2012/06/10/how-to-fix-error-design-time-data-in-blend-with-mvvm-光/

错误消失了,但现在我在 Visual Studio 和 Blend 中都看不到任何设计数据。

我的视图模型定位器(IContentService不引发错误,只有IFeedAPI):

当我不使用 IoC 容器时,如下所示,一切正常(没有错误并且完全设计数据)。

有任何想法吗??

0 投票
1 回答
180 浏览

mvvm-light - 巨大的 MVVMLight ViewModelLocator

我有一个主项目 A,它有许多依赖项,如项目 B、项目 C 等......它们都是程序集/dll。目前我在 ProjectA.App.xaml 中有一个 ViewModelLocator。这是 MVVMLight 推荐的方式。ViewModelLocator 工作正常,但我的问题是它与来自 ProjectB、ProjectC 等的所有 ViewModel 引用都很庞大......而且很难维护。

我正在寻找一种将 ViewModelLocator 隔离到每个项目 ProjectB、ProjectC 等的解决方案,并拥有自己的 ViewModelLocator。我还想删除 ViewModelLocator 的全局引用,这是 MVVMLight 推荐的方式。

有任何想法吗?