本文介绍了缺少 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显示错误:

错误 1 ​​名称空间中不存在名称EventToCommand"clr 命名空间:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8".

错误 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,而 ToggleSwitchDataTemplate 的一部分.我使用这一行包括 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"

怎么了?为什么我得到那个错误?我试图使用谷歌,但找不到解决方案.

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 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 01:17