简体   繁体   中英

WP8 MvvmLight namespace missing and EventToCommand doesn't exist

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.

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).

在此处输入图片说明

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM