【问题标题】:Cannot create an instance of "ViewModelLocator"无法创建“ViewModelLocator”的实例
【发布时间】:2012-01-29 08:19:00
【问题描述】:

我在 Visual Studio 2010 和 Blend 4 中的 silverlight 4 Windows Phone 7 项目中使用 MVVM Light 已经有一段时间了。 然后突然我开始在 VS 2010 和 Blend 中收到“无法创建“ViewModelLocator”的实例”错误。无法弄清楚为什么它现在应该出现。如果某个地方发生了一些我没有发现的变化,我如何追踪我的任何想法。 我所做的唯一更改,然后放回去,是在其中一个用户控件的数据上下文表达式前面添加一个“d:”。我有设计中绑定的想法,但在运行时以延迟方式以编程方式绑定。

我看到其他人发布了这个问题,但答案是指 Blend 中的一个错误,该错误显然已得到修复。这也发生在 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>

【问题讨论】:

  • 添加一些 App.xaml sn-p 代码? (初始化您的 ViewModelLocator + 命名空间声明)

标签: windows-phone-7 silverlight-4.0 mvvm mvvm-light blend


【解决方案1】:

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

我用的是

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

为设计时填充一些值。我会通过设置它们的属性来启动模型中的值。这一切都很好,直到我开始根据属性更改添加更复杂的事件处理例程。

为了纠正这个问题并恢复我的“可混合性”,我做了两件事!

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

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    以前是answered here。验证问题不是对象实例创建期间的错误(构造函数等

    【讨论】:

    • 感谢您,但我已尝试删除 ViewModel 中的所有启动代码。我现在只是忽略了这个错误。该应用程序构建和部署良好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2011-09-09
    • 2021-05-17
    • 2016-08-24
    • 1970-01-01
    相关资源
    最近更新 更多