0

I am using WPF MVVM Light for one of my application. I found an error when I debugging my code is "EventToCommand.cs not found" on Window Loaded Command.

My Code is :

   xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
   xmlns:command="http://www.galasoft.ch/mvvmlight"

  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing" >
        <command:EventToCommand Command="{Binding WindowCloseCommand}" PassEventArgsToCommand="True"  />
    </i:EventTrigger>
    <i:EventTrigger EventName="Loaded" >
        <command:EventToCommand Command="{Binding WindowLoadedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor}}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

In View Model:

  public ICommand WindowLoadedCommand
    {
        get { return _windowLoadedCommand ?? (_windowLoadedCommand = new RelayCommand<MetroWindow>(OnWindowLoaded)); }
    }

  public void OnWindowLoaded(MetroWindow window)
  {

  }

Please help me to find out solution for this issue.

Dharmesh
  • 107
  • 1
  • 16
  • 1
    What are the values of `i` and `command` namespaces? – Sridhar Jul 09 '15 at 10:11
  • xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:command="http://www.galasoft.ch/mvvmlight" – Dharmesh Jul 09 '15 at 10:15
  • Looks like someone had a similar problem. There's a solution here. See if it works. http://stackoverflow.com/questions/14301527/eventtocommand-dont-exist-in-mvvmlight-toolkit-wpf4-5 – Sridhar Jul 09 '15 at 10:25
  • You need to learn how WPF maps namespaces. That "clr-namespace:" stuff isn't just cargo cult, it's necessary. Here's one of my answers with some of the details about how this works http://stackoverflow.com/questions/8852912/xamlparseexception-in-view/8882539#8882539 –  Jul 09 '15 at 14:02

2 Answers2

1

I had the same problem, this ended up solving it for me:

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

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Tap" >                
        <Command:EventToCommand  Command="{Binding Path=MyVM.MyCommand, Source={StaticResource Locator}}" 
                                 PassEventArgsToCommand="False"    
                                 CommandParameter="{Binding}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
D.Rosado
  • 5,634
  • 3
  • 36
  • 56
0
Use following code it might be work because in my case it's working.



<i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding WindowLoadedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

ViewModel.cs,



   public ICommand WindowLoadedCommand
    {
        get { return new RelayCommand<object>(WindowLoadedCommandExecute); }

    }
    public void WindowLoadedCommandExecute(object obj)
    {

    }
Himalaya
  • 81
  • 7