Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 248
Libor Zapletal Avatar asked Dec 11 '14 18:12

Libor Zapletal


1 Answers

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.

like image 127
Justin XL Avatar answered Nov 04 '22 05:11

Justin XL