尝试运行WP8应用程序时获取System.Windows.Markup.XamlParseException
我正在编写Windows Phone 8应用程序并使用MVVM light。我已经将WP8应用程序的ViewModel和Model类编写到单独的PCL项目中 使用表达式混合时,它会正确填充设计时数据。但当我尝试在emulator中运行应用程序时,会出现以下错误。你能帮我找出这个错误的解决办法吗尝试运行WP8应用程序时获取System.Windows.Markup.XamlParseException,xaml,windows-phone-8,mvvm-light,Xaml,Windows Phone 8,Mvvm Light,我正在编写Windows Phone 8应用程序并使用MVVM light。我已经将WP8应用程序的ViewModel和Model类编写到单独的PCL项目中 使用表达式混合时,它会正确填充设计时数据。但当我尝试在emulator中运行应用程序时,会出现以下错误。你能帮我找出这个错误的解决办法吗 A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Window
A first chance exception of type 'System.Windows.Markup.XamlParseException'
occurred in System.Windows.ni.dll
以下是例外情况的详细信息
System.Windows.Markup.XamlParseException occurred
HResult=-2146233087
Message=Cannot create instance of type 'MyPkg.Commons.ViewModel.ViewModelLocator' [Line: 12 Position: 61]
Source=System.Windows
LineNumber=12
LinePosition=61
StackTrace:
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at MyPkg.WindowsPhone8.App.InitializeComponent()
at MyPkg.WindowsPhone8.App..ctor()
InnerException: System.TypeInitializationException
HResult=-2146233036
Message=The type initializer for 'MyPkg.Commons.ViewModel.ViewModelLocator' threw an exception.
Source=mscorlib
TypeName=MyPkg.Commons.ViewModel.ViewModelLocator
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at MS.Internal.TypeProxy.<>c__DisplayClass32.<GetCreateObjectDelegate>b__2c()
at MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
at MS.Internal.XamlManagedRuntimeRPInvokes.CreateInstance(XamlTypeToken inXamlType, XamlQualifiedObject& newObject)
InnerException: System.IO.FileLoadException
HResult=-2146234304
Message=Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Source=MyPkg.Commons
StackTrace:
at MyPkg.Commons.ViewModel.ViewModelLocator..cctor()
InnerException:
发生System.Windows.Markup.XamlParseException
HResult=-2146233087
Message=无法创建“MyPkg.Commons.ViewModel.ViewModelLocator”类型的实例[行:12位置:61]
Source=System.Windows
行号=12
LinePosition=61
堆栈跟踪:
位于System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator)
在MyPkg.WindowsPhone8.App.InitializeComponent()中
在MyPkg.WindowsPhone8.App..ctor()上
InnerException:System.TypeInitializationException
HResult=-21462036
Message=MyPkg.Commons.ViewModel.ViewModelLocator的类型初始值设定项引发异常。
Source=mscorlib
TypeName=MyPkg.Commons.ViewModel.ViewModelLocator
堆栈跟踪:
位于System.RuntimeMethodHandle.InvokeMethod(对象目标、对象[]参数、签名符号、布尔构造函数)
在System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr、Binder Binder、Object[]参数、CultureInfo区域性)
在MS.Internal.TypeProxy.c__DisplayClass32.b__2c()中
位于MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)
位于MS.Internal.XamlManagedRuntimeRPInvokes.CreateInstance(XamlTypeToken inXamlType、XamlQualifiedObject和newObject)
InnerException:System.IO.FileLoadException
HResult=-2146234304
Message=无法加载文件或程序集“Microsoft.Practices.ServiceLocation,版本=1.0.0.0,区域性=中性,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集清单定义与程序集引用不匹配。(来自HRESULT的异常:0x8013100)
Source=MyPkg.Commons
堆栈跟踪:
在MyPkg.Commons.ViewModel.ViewModelLocator..cctor()中
内部异常:
下面是App.xaml
<Application 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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:MyPkg.Commons.ViewModel;assembly=MyPkg.Commons"
mc:Ignorable="d" x:Class="MyPkg.WindowsPhone8.App">
<!--Application Resources-->
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:MyPkg.WindowsPhone8" x:Key="LocalizedStrings" />
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated" />
</Application.ApplicationLifetimeObjects>
</Application>
下面是ViewModelLocator的代码构造函数
静态ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default);
if(ViewModelBase.IsIndesignatic)
{
//创建设计时视图服务和模型
SimpleIoc.Default.Register();
}
其他的
{
//创建运行时视图服务和模型
SimpleIoc.Default.Register();
}
SimpleIoc.Default.Register();
}
您对Microsoft.Practices.ServiceLocation程序集的引用似乎有问题。打开项目的“引用”文件夹,右键单击Microsoft.Practices.ServiceLocation,然后选择“属性”。确保路径处引用的文件存在,并且“复制本地”设置为True,然后重新生成应用程序。如果此错误仍然存在,请删除MVVM Light库并将其重新添加到您的项目中。谢谢,commons(我的PCL库)和wp8 project引用的是不同版本的Microsoft.Practices.ServiceLocation。我让这两个版本都参考了Microsoft.Practices.ServiceLocation的PCL版本,它开始工作了。