我在Visual Studio2010和Blend 4中的Silverlight4Windows phone7项目中使用MVVM Light很好地工作了一段时间。然后突然我开始在VS2010和Blend中收到"Cannot create an instance of "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>
发布于 2012-01-05 19:07:54
多亏了上面的建议,我开始研究我的ViewModel的构造函数。虽然我没有错误,但我确实发现belnd在事件侦听器和处理程序方面有问题。
我使用的是
if (IsInDesignMode)
{
//populate values here for blend
}
else
{
//runtime initiation
}为设计时填充一些值。我会通过设置它们的属性来初始化模型中的值。这一切都很好,直到我开始添加基于属性更改的更复杂的事件处理例程。
为了纠正这一点并恢复我的“可调性”,我做了两件事!
希望这能有所帮助!
发布于 2011-12-30 03:37:37
这在以前就是answered here。验证该问题不是创建对象实例(构造函数等)过程中的错误
https://stackoverflow.com/questions/8658967
复制相似问题