c# - XML 命名空间 clr-命名空间 :XXX 中不存在标记 'ViewModelLocator'

标签 c# windows-phone-8 portable-class-library

我尝试过许多其他解决方案,但均未成功。我有一门课叫 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 知道这些对象,但是当我尝试构建时,我收到以下错误:

The tag 'ViewModelLocator' does not exist in XML namespace 'clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable'.

The tag 'SampleViewModel' does not exist in XML namespace 'clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable'.

谁能提供一些帮助?

[更新] 我在我的 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 时出现问题?! [更新]

非常感谢! 亲切的问候,

最佳答案

我刚刚在 .Net 4.5 项目中遇到了这个问题。 我的解决方案是更改为 .Net 4.0,忽略警告,然后改回 .Net 4.5。 然后问题就解决了。

不知道这对其他人是否可行,但它对我有用。

最好的问候。

关于c# - XML 命名空间 clr-命名空间 :XXX 中不存在标记 'ViewModelLocator',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18257660/

相关文章:

xamarin.ios - 代码适用于 Android 和 iOS - 为什么我不能将它移动到 PCL 中?

c# - 在实践中,Contexts(Entity Framework)应该用在controller中还是在另一个抽象层?

c# - 拆箱到未知类型

c# - 如果命令绑定(bind)解析为空,为什么启用按钮?

c# - 如何通过LongListSelector索引选择ItemViewModel对象

c# - "Symbols for the module MyLibrary.dll were not loaded"?

sqlite - 如何在SQLite Select语句中正确表达参数

c# - 如何使用 PCL 支持从 xamarin 跨平台项目中的 URL 下载文件

c# - 默认 Azure 移动应用程序后端返回 500

c# - 我不能将动态与可移植类库一起使用吗?