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

发布于 2024-12-14 02:47:13 字数 1276 浏览 6 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(1

半城柳色半声笛 2024-12-21 02:47:13

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

You cannot useSource={DynamicResource Locator} in your binding. If you use the Source property, you need to use a StaticResource

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