WP8 MvvmLight namespace missing and EventToCommand doesn't exist

ぐ巨炮叔叔 提交于 2020-06-25 10:15:06

问题


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>

The problem is that VS shows errors:

Error 1 The name "EventToCommand" does not exist in the namespace "clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8".

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.

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

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.


回答1:


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.

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

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

enter image description here

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.




回答2:


Why not use Microsoft.Behaviors SDK ? (references, add reference, extensions, behavior sdk) Not sure but I think EventTrigger and mvvm light EventToCommand is deprecated now (because of behaviors sdk).

Code sample with Behaviors.SDK:

xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

<toolkit:ToggleSwitch x:Name="LockSwitch"
        IsChecked="{Binding IsLock, Mode=TwoWay}">
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="Toggled">
                    <core:InvokeCommandAction Command="{Binding command}" CommandParameter="{Binding param}"/>
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
</toolkit:ToggleSwitch>


来源:https://stackoverflow.com/questions/27430160/wp8-mvvmlight-namespace-missing-and-eventtocommand-doesnt-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!