Windows phone 7 无法创建“”的实例;ViewModelLocator";

Windows phone 7 无法创建“”的实例;ViewModelLocator";,windows-phone-7,silverlight-4.0,mvvm,mvvm-light,blend,Windows Phone 7,Silverlight 4.0,Mvvm,Mvvm Light,Blend,在VisualStudio2010和Blend 4中的silverlight 4 Windows Phone 7项目中,我一直与MVVM Light配合得很好。 然后突然,我开始在VS2010和Blend中出现“无法创建“ViewModelLocator”实例”错误。我不明白为什么它现在会出现。如果某个地方发生了我没有发现的变化,我怎么可能找到它。 我所做的唯一更改是在一个用户控件中的数据上下文表达式前面添加一个“d:”。我的想法是在设计时绑定,但在运行时以延迟方式编程绑定 我看到其他人发布了这

在VisualStudio2010和Blend 4中的silverlight 4 Windows Phone 7项目中,我一直与MVVM Light配合得很好。 然后突然,我开始在VS2010和Blend中出现“无法创建“ViewModelLocator”实例”错误。我不明白为什么它现在会出现。如果某个地方发生了我没有发现的变化,我怎么可能找到它。 我所做的唯一更改是在一个用户控件中的数据上下文表达式前面添加一个“d:”。我的想法是在设计时绑定,但在运行时以延迟方式编程绑定

我看到其他人发布了这个问题,但答案提到了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>


这一点以前已经提到过。验证在创建对象实例(构造函数等)时该问题不是错误。

由于上述建议,我开始研究ViewModel的构造函数。虽然我没有错误,但我确实发现belnd在事件侦听器和处理程序方面存在问题

我用的是

if (IsInDesignMode)
{
   //populate values here for blend
}
else
{
   //runtime initiation
}
为设计时填充一些值。我将通过设置它们的属性来启动模型中的值。在我开始基于属性更改添加更复杂的事件处理例程之前,这一切都很好

为了纠正这一点并恢复我的“可混合性”,我做了两件事

  • 在IsDesignMode部分中设置专用字段而不是属性。这样可以避免触发PropertyChanged事件
  • 在事件处理程序中添加了IsInDesignMode检测,该检测仍然存在问题,并跳过了任何级联更新

  • 希望这有帮助

    添加一些App.xaml代码片段?(这初始化了ViewModelLocator+名称空间衰减)谢谢,但我已经尝试删除ViewModel中的所有启动代码。我现在只是忽略了这个错误。该应用程序构建和部署良好!