2011-12-28 39 views
2

我一直在使用Visual Studio 2010中的Silverlight 4 Windows Phone 7項目和Blend 4進行MVVM Light的工作。 然後突然我開始在VS 2010和Blend中都得到「無法創建」ViewModelLocator「的實例」錯誤。無法弄清楚它現在應該出現的原因。任何關於我如何能夠追蹤某個地方發生了什麼改變但我還沒有發現的想法。 我做的唯一更改,然後放回去,是在用戶控件之一中添加數據上下文表達式的「d:」infront。我有設計綁定的想法,但在運行時以編程方式綁定。無法創建「ViewModelLocator」的實例

我看到其他人發佈了這個問題,但答案指的是Blend中的一個bug,它顯然已被修復。這也是在VS2010中發生的。

的App.xaml看起來像

<Application x:Class="BillSplitter2.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     xmlns:vm="clr-namespace:BillSplitter2.ViewModel" 
     xmlns:converters="clr-namespace:HardMediumSoft.WP7.Tools.Converters;assembly=HardMediumSoft.WP7.Tools"> 


<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Resources/ResourceDictionary1.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
     <!--Global View Model Locator--> 
     <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/> 
     <!--Localization String Library--> 
     <local:LocalizedStrings xmlns:local="clr-namespace:BillSplitter2.Utilities" 
           x:Key="LocalizedStrings" /> 
     <!--Converters --> 
     <converters:FloatConverter x:Key="FloatConverter" /> 
     <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" /> 
     <converters:StringToBrush x:Key="StringToBrushConverter" /> 
    </ResourceDictionary> 
</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> 

+1

添加一些App.xaml代碼片段? (初始化您的ViewModelLocator +名稱空間十進制) – vidalsasoon

回答

2

感謝上面的建議,我開始研究ViewModel的構造函數。儘管我沒有錯誤,但我確實發現belnd與事件監聽器和處理程序有關。

我用的是

if (IsInDesignMode) 
{ 
    //populate values here for blend 
} 
else 
{ 
    //runtime initiation 
} 

來填充設計時的一些值。我會通過設置屬性來啓動模型中的值。直到我開始根據屬性更改添加更復雜的事件處理例程時,情況一切正常。

爲了糾正這種情況並恢復我的「可調整性」,我做了兩件事!

  1. 在IsDesignMode部分設置私有字段而不是屬性。這避免了PropertyChanged事件觸發。
  2. 在仍然存在問題的事件處理程序中添加了IsInDesignMode檢測並跳過了任何級聯更新。

希望這有助於!

1

這在以前是answered here。驗證在創建對象實例期間問題不是錯誤(構造函數等。

+0

謝謝,但我已經嘗試刪除ViewModel中的所有啓動代碼。我現在只是無視錯誤。該應用程序的構建和部署很好! – NER1808