首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法创建"ViewModelLocator“实例

无法创建"ViewModelLocator“实例
EN

Stack Overflow用户
提问于 2011-12-29 01:08:14
回答 2查看 3.4K关注 0票数 2

我在Visual Studio2010和Blend 4中的Silverlight4Windows phone7项目中使用MVVM Light很好地工作了一段时间。然后突然我开始在VS2010和Blend中收到"Cannot create an instance of "ViewModelLocator"“错误。我搞不懂为什么它会出现在现在。如果某个地方发生了一些我没有发现的变化,我该如何追踪呢?我所做的唯一更改是在其中一个用户控件中的数据上下文表达式前面添加了一个"d:“。我的想法是在设计中绑定,但在运行时以延迟的方式以编程方式绑定。

我看到其他人也发布了这个问题,但答案是Blend中的一个bug,显然已经修复了。同样,这也发生在VS2010中。

App.xaml看起来像

代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-05 19:07:54

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

我使用的是

代码语言:javascript
复制
if (IsInDesignMode)
{
   //populate values here for blend
}
else
{
   //runtime initiation
}

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

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

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

希望这能有所帮助!

票数 2
EN

Stack Overflow用户

发布于 2011-12-30 03:37:37

这在以前就是answered here。验证该问题不是创建对象实例(构造函数等)过程中的错误

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8658967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档