2013-09-24 30 views
0

我正在編寫Windows Phone 8應用程序並使用MVVM指示燈。我將WP8應用程序的ViewModel和Model類寫入了單獨的PCL項目。嘗試運行WP8應用程序時獲取System.Windows.Markup.XamlParseException

使用Expression Blend時,它可以正確填充設計時間數據。但是當我嘗試在模擬器中運行應用程序時,它會出現以下錯誤。你能幫我解決這個錯誤的解決方法嗎?

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: 

下面的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

static ViewModelLocator() 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     if (ViewModelBase.IsInDesignModeStatic) 
     { 
      // Create design time view services and models 
      SimpleIoc.Default.Register<IAlaramService, MyPkg.Commons.Design.AlaramService>(); 
     } 
     else 
     { 
      // Create run time view services and models 
      SimpleIoc.Default.Register<IAlaramService, MyPkg.Commons.Design.AlaramService>(); 
     } 

     SimpleIoc.Default.Register<MainViewModel>(); 
    } 

回答

1

的代碼構造看起來有一個與你的參考Microsoft.Practices.ServiceLocation裝配問題。打開項目的References文件夾,右鍵單擊Microsoft.Practices.ServiceLocation,然後選擇屬性。確保Path中引用的文件存在,並且將Copy Local設置爲True,然後重新構建應用程序。如果此錯誤仍然存​​在,請將MVVM Light庫刪除並重新添加到您的項目中。

+1

謝謝,commons(我的PCL庫)和wp8項目指的是不同版本的Microsoft.Practices.ServiceLocation。我讓兩者都參考了Microsoft.Practices.ServiceLocation的PCL版本,並開始工作。 –

相關問題