【发布时间】:2024-01-02 00:51:01
【问题描述】:
我尝试了许多其他解决方案,但均未成功。我有一个名为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 命名空间中不存在标签“ViewModelLocator” 'clr-namespace:Foo.Core.Portable.ViewModel;assembly=Foo.Core.Portable'。
XML 命名空间中不存在标记“SampleViewModel” '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 有问题吗?!
[更新]
非常感谢提前! 亲切的问候,
【问题讨论】:
-
您是否已经尝试在构建之前执行清理?
-
ViewModelLocator和SampleViewModel是否声明为public?另外,您的项目是否正确引用了Foo.Core.Portabledll?您的应用程序可以访问该库吗? -
@Steve - 感谢您的回复!是的,我不止一次清理了项目/解决方案。这并没有解决问题。
-
@SuperOli - 是的,课程和财产都是公开的。您的项目正确引用了吗?你能详细说明一下吗?我通过从参考管理器(解决方案选项卡)中选择项目参考来添加它。是的 - 它是可访问的,它是同一解决方案的一部分。
-
在 NuGet 包管理器中确保 MVVMLight、NuGet 本身以及可能的 System.Composition 都是最新的。似乎 MVVMLight 目前可能无法正确处理此问题。
标签: c# windows-phone-8 portable-class-library