4

我尝试了许多其他解决方案,但都没有成功。我有一个名为的类ViewModelLocator,它位于我的可移植类库中。它有一个名为 的属性,它ViewModels的类型是Dictionay<K, V>

然后我有一个引用可移植类库的 Windows Phone 8 项目。我在 WP8 app.xaml 中添加了以下内容:

<Application
    x:Class="Kaizen.WP8.Test.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:test="clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable">
    <Application.Resources>

        <test:ViewModelLocator x:Key="ViewModelLocator">
            <test:ViewModelLocator.ViewModels>
                <test:SampleViewModel x:Key="sampleVM"/>
            </test:ViewModelLocator.ViewModels>
        </test:ViewModelLocator>
    </Application.Resources>
</Application>

当我在标签上按 F12 时,它会导航到我的 pcl 中正确的类和/或属性。这表明 VS 知道对象,但是当我尝试构建时,我收到以下错误:

XML 命名空间“clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable”中不存在标记“ViewModelLocator”。

XML 命名空间“clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable”中不存在标记“SampleViewModel”。

有人可以提供一些帮助吗?

[更新] 我在我的 pcl 项目中引用了 mvvm light 的 pcl 版本。这是ViewModelLocator类的样子:

public class ViewModelLocator
{
    public dynamic this[string viewModelName]
    {
        get
        {
            if (this.ViewModels.ContainsKey(viewModelName))
            {
                return this.ViewModels[viewModelName];
            }

            else
            {
                return null;
            }
        }
    }

    public Dictionary<string, ViewModelBase> ViewModels { get; set; }

    public ViewModelLocator()
    {
        this.ViewModels = new Dictionary<string, ViewModelBase>();
    }
}

我的 WP8 项目也使用了 mvvm light pcl 程序集。我注意到,如果我将ViewModelBase类用作字典值,​​那么当我得到错误时。这是因为在两个项目之间使用 mvvm light pcl 有问题吗?! [更新]

提前谢谢了!!亲切的问候,

4

3 回答 3

4

I just had this problem with a .Net 4.5 project. The solution for me was to change to .Net 4.0, ignore the warnings, and change back to .Net 4.5. Then the problem was gone.

Don't know if it is a feasible way for others, but it worked for me.

Best regards.

于 2014-02-11T17:11:32.477 回答
2

好的,所以我不确定我在第一次尝试中做错了什么,但是我重新创建了解决方案并执行了或多或少相同的步骤并且我没有再次收到错误?!o_O

于 2013-10-03T06:40:41.053 回答
0

I know this is a bit late but I had the same problem with a WPF Desktop app and a control library. The library's default Target Framework was .Net 4 but the Desktop app just after I created in in Visual Studio was by default created with .Net 4 client profile. I changed the Desktop app from .Net 4 client profile to .Net 4 and it worked.

于 2013-12-10T17:34:51.467 回答