使用 MVVM light 在两个 WPF 应用程序之间共享视图
使用 MVVM Light,我有两个引用通用视图库的 WPF 应用程序。我还有一个 ViewModels 库。 ViewModels 库有一个 ViewModelLocator。
依赖关系非常简单: WPF 应用程序 ->浏览次数-> ViewModels
Views 库有一个 ResourceDictionary 并定义了一个 ViewModelLocator 资源,用于在运行时和设计时进行数据绑定:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ViewModels;assembly=ViewModels">
<vm:ViewModelLocator x:Key="Locator"/>
</ResourceDictionary>
问题是,当我在视图的顶层元素设置 DataContext 时,出现异常:
<UserControl x:Class="Views.WelcomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding WelcomeViewModel, Source={DynamicResource Locator}}">
<Grid>
<TextBlock Text="{Binding Text}"/>
</Grid>
</UserControl>
异常: 无法在“Binding”类型的“Source”属性上设置“DynamicResourceExtension”。 “DynamicResourceExtension”只能在 DependencyObject 的 DependencyProperty 上设置
我做错了什么?将视图中的定位器定义为资源是否是最好的方法?
Using MVVM Light, I have two WPF applications that reference a common Views library. I also have a ViewModels library. The ViewModels library has a ViewModelLocator.
The dependencies are pretty simple:
WPF apps -> Views -> ViewModels
The Views library has a ResourceDictionary and defines a ViewModelLocator resource for data binding at runtime and design time:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:ViewModels;assembly=ViewModels">
<vm:ViewModelLocator x:Key="Locator"/>
</ResourceDictionary>
The problem is that when I go to set the DataContext at the top level element of my Views, I get an exception:
<UserControl x:Class="Views.WelcomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding WelcomeViewModel, Source={DynamicResource Locator}}">
<Grid>
<TextBlock Text="{Binding Text}"/>
</Grid>
</UserControl>
Exception:
A 'DynamicResourceExtension' cannot be set on the 'Source' property of type 'Binding'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject
What am I doing wrong? Is defining the Locator in the Views as a resource even the best approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在绑定中使用
Source={DynamicResource Locator}
。如果您使用Source
属性,则需要使用StaticResource
You cannot use
Source={DynamicResource Locator}
in your binding. If you use theSource
property, you need to use aStaticResource