本文介绍了WP8 MvvmLight名称空间缺失,EventToCommand不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Phone 8项目中仅使用MVVM Light库(来自Nuget包),并且我想在 ToggleSwitch 中使用 EventToCommand .我有以下几行代码:

I am using MVVM Light libraries only (from Nuget package) in my Windows Phone 8 project and I want to use EventToCommand in ToggleSwitch. I have these lines of codes:

<toolkit:ToggleSwitch x:Name="LockSwitch"
        IsChecked="{Binding IsLock, Mode=TwoWay}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Toggled">
            <Command:EventToCommand 
                Command="{Binding DataContext.NavigateToArticleCommand, ElementName=LayoutRoot}"
                CommandParameter="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</toolkit:ToggleSwitch>

问题在于VS显示错误:

The problem is that VS shows errors:

错误2找不到类型'Command:EventToCommand'.验证您不会缺少程序集引用,并且所有引用都被引用程序集已构建.

Error 2 The type 'Command:EventToCommand' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

错误3 XML名称空间中不存在标签'EventToCommand''clr-namespace:GalaSoft.MvvmLight.Command; assembly = GalaSoft.MvvmLight.Extras.WP8'.

Error 3 The tag 'EventToCommand' does not exist in XML namespace 'clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8'.

我上面的文件Styles.xaml中有几行,这是 ResourceDictionary ,而 ToggleSwitch DataTemplate 的一部分.我使用此行包括MvvmLight库:

I have lines above in file Styles.xaml which is a ResourceDictionary and ToggleSwitch is part of a DataTemplate. I am including MvvmLight library using this line:

xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"

怎么了?为什么我会收到该错误?我试图使用Google,但找不到解决方案.

What's wrong? Why I get that error? I was trying to use google but I couldn't find a solution.

推荐答案

用于包含命令的引用是错误的.正确的参考是

The reference that you use to include the command is wrong. The correct reference is

xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"


有一个技巧可以在不编写任何代码的情况下获得此引用.


There's a trick to obtain this reference without writing a single line of code.

下载MvvmLight nuget程序包后,编译项目,然后在Expression Blend中打开xaml文件.

After you have downloaded the MvvmLight nuget package, compile your project and then open your xaml file in Expression Blend.

然后单击左侧工具栏上的资产图标(底部),然后开始输入"eventtocommand"(请参见下图).

Then click the Assets icon on the left toolbar (the bottom one) and start typing "eventtocommand" (see picture below).

一旦您看到 EventToCommand 出现在 Assets 面板中,请将其拖放到您的 ToggleSwitch 顶部.就是这样!该引用将以及实际的命令代码自动添加到您的xaml中.

Once you see EventToCommand appear in the Assets panel, drag and drop it on top of your ToggleSwitch. That's it! The reference will be added into your xaml automatically as well as the actual command code.

这篇关于WP8 MvvmLight名称空间缺失,EventToCommand不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:14