使用MVVM灯在两个WPF应用程序之间共享视图

时间:2011-11-08 15:45:27

标签: wpf xaml data-binding mvvm mvvm-light

使用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>

问题是,当我在我的Views的顶级元素中设置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>

例外: 无法在“绑定”类型的“Source”属性上设置“DynamicResourceExtension”。 'DynamicResourceExtension'只能在DependencyObject的DependencyProperty上设置

我做错了什么?是否将视图中的定位器定义为资源,即使是最好的方法?

1 个答案:

答案 0 :(得分:3)

您无法在绑定中使用Source={DynamicResource Locator}。如果您使用Source属性,则需要使用StaticResource

相关问题