另一个MVVMLight项目中的MVVMLight [英] MVVMLight within another MVVMLight project

查看:106
本文介绍了另一个MVVMLight项目中的MVVMLight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MVVMLight/WPF项目中工作,需要添加大量功能,其中将包括多个视图和视图模型.我知道在不久的将来其他项目将使用相同的功能,因此我希望将此功能作为自己的项目,我可以根据需要将其添加到其他解决方案中,而几乎不需要修改.

I am working on a MVVMLight / WPF project and need to add a chunk of functionality which will include multiple views and viewmodels. I know this same bit of functionality will be used in other projects in the near future so I would like to make this functionality its own project that I can add to other solutions as needed wiuth little or no modification.

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

I started by adding a second MVVMLight project (Beta), removing the standard MainWindow.xaml and MainViewModel.cs files and created a simple UserControl and associated View Model.

<UserControl x:Class="Beta.View.TestView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ignore="http://www.ignore.com"
        mc:Ignorable="d ignore"
        DataContext="{Binding Test_VM, Source={StaticResource Locator} }">

    <Grid>
        <TextBlock Text="{Binding WelcomeMessage}" />
    </Grid>
</UserControl>



public class TestViewModel : ViewModelBase
{
    #region Properties

    public string WelcomeMessage
    {
        get
        {
            return "Hello World!";
        }
    }

    #endregion Properties

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the TestViewModel class.
    /// </summary>
    public TestViewModel()
    {

    }

    #endregion Constructors
}

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

I am able to add Beta as a reference to the original project (Alpha) and display the view by inserting the view into a stack panel like so:

<StackPanel Name="MasterStackPanel"
            DockPanel.Dock="Top">
    <beta:TestView />
</StackPanel>

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

Everything appears to work properly when doing this. The issue I am having is when I try to bind a Property from TestViewModel to TestView.

在TestView中,如果我这样做:

In TestView, if I do this:

<TextBlock Text="Hello World" />

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

the TestView displays correctly at runtime. But when I bind the TextBlock to a property like so:

<TextBlock Text="{Binding WelcomeMessage}" />

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

The message does not display and the locator for Beta appears to be ignored (the datacontext is not being bound) and I am getting the following error from Snoop:

System.Windows.Data Error: 40 : BindingExpression path error: 'WelcomeMessage' property not found on 'object' ''MainViewModel' (HashCode=51013215)'. BindingExpression:Path=WelcomeMessage; DataItem='MainViewModel' (HashCode=51013215); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Test_VM' property not found on 'object' ''ViewModelLocator' (HashCode=22749765)'. BindingExpression:Path=Test_VM; DataItem='ViewModelLocator' (HashCode=22749765); target element is 'TestView' (Name=''); target property is 'DataContext' (type 'Object')

我认为这意味着Test_VM&试图通过Alpha定位器而不是Beta定位器找到WelcomeMessage.我正在使用在每个项目中启动MVVMLight项目时默认创建的ViewModelLocator.

I believe this means that the binding of Test_VM & WelcomeMessage are trying to be found via the Alpha Locator and not the Beta Locator. I am using the ViewModelLocator that is created by default when starting a MVVMLight project in each project.

是否可能有第二个定位器",如果需要,我该怎么做才能使其正常工作?

Is it possible to have an second 'Locator' and if so what do I need to do to make it work?

推荐答案

我认为您应该在系统的应用程序根目录中仅具有一个Locator,并使用库项目中的"MvvmLightLibs"库并在alpha项目中引用它,然后在定位器中添加一个TestViewModel-Property.

I think you should only have one Locator in the application root of the system and use the "MvvmLightLibs" library in the library project and reference it in the alpha project and add a TestViewModel-Property in the locator.

这篇关于另一个MVVMLight项目中的MVVMLight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆