title: How to turn Event into Command in Xamarin Forms MVVM
body: <p><strong>Tools:</strong> I am using Xamarin forms &amp; MVVM</p>
  <p>I am following video: <a href="https://www.youtube.com/watch?v=7mpe_1okwxk&amp;list=PLwOF5UVsZWUiHY1CkRVjYJ6dm0iCvAlfw&amp;index=12" rel="nofollow noreferrer">https://www.youtube.com/watch?v=7mpe_1okwxk&amp;list=PLwOF5UVsZWUiHY1CkRVjYJ6dm0iCvAlfw&amp;index=12</a></p>
  <p><strong>NuGet:</strong> I have installed NuGet &gt; xamarin.community.toolkit</p>
  <p><strong>Error:</strong></p>
  <blockquote>
  <p>System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'</p>
  </blockquote>
  <p>Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).</p>
  <p><em><strong>Debug:</strong></em></p>
  <p>I am trying to Convert Collection ItemSelect Event Into a Command. I know issue is def with code that is trying to convert event to command But I am not sure how to go about debugging this error. Since app has entered in break state, I can't simple add break points. I did copy/paste outfile at bottom of this topic</p>
  <p>Note if I remove code that convert event to command, that it works fine. So this means issue is with: <code>CollectionView.Behaviors</code> or <code>xct:ItemSelectedEventArgsConverter</code></p>
  <p><strong>View Page</strong></p>
  <pre><code>&lt;ContentPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
           x:Class=&quot;TestApp02_MVVM_Product.Views.ProductPage&quot;
           xmlns:xct=&quot;http://xamarin.com/schemas/2020/toolkit&quot;
           xmlns:android=&quot;clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core&quot;&gt;
  
  &lt;ContentPage.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;xct:ItemSelectedEventArgsConverter x:Key=&quot;ItemSelectedEventArgsConverter&quot; /&gt;
      &lt;/ResourceDictionary&gt;    
  &lt;/ContentPage.Resources&gt;
  
  &lt;ContentPage.Content&gt;
          &lt;CollectionView ItemsSource=&quot;{Binding ProductsList}&quot;
                              SelectedItem=&quot;{Binding ItemSelectedEvent, Mode=TwoWay}&quot;
                              ...&gt;
                  &lt;!-- Turn Event Into Command--&gt;
                  &lt;CollectionView.Behaviors&gt;
                      &lt;xct:EventToCommandBehavior 
                          EventName=&quot;ItemSelected&quot;
                          Command=&quot;{Binding ItemSelectedCommand}&quot;
                          EventArgsConverter=&quot;{StaticResource ItemSelectedEventArgsConverter}&quot;/&gt;
                  &lt;/CollectionView.Behaviors&gt;
                  ...
          &lt;/CollectionView&gt;
  ...
  </code></pre>
  <p><em><strong>ViewModel</strong></em></p>
  <pre><code>using System.ComponentModel;
  using System.Runtime.CompilerServices;
  using TestApp02_MVVM_Product.Models;
  using Xamarin.CommunityToolkit.ObjectModel;
  using Xamarin.Forms;
  using System.Threading.Tasks;
  
  namespace TestApp02_MVVM_Product.ViewModels
  {
      class ProductViewModel : INotifyPropertyChanged
      {
          public ObservableRangeCollection&lt;ProductModel&gt; ProductsList { get; set; }
          public AsyncCommand&lt;ProductModel&gt; ItemSelectedCommand { get; }
          
           public ProductViewModel()
          {
              ProductsList = new ObservableRangeCollection&lt;ProductModel&gt;();
              ProductsList.Add(new ProductModel { ProductId = 1, ProductName = &quot;Name 1&quot;, ProductDescription = &quot;Description 1&quot; });
              ProductsList.Add(new ProductModel { ProductId = 1, ProductName = &quot;Name 1&quot;, ProductDescription = &quot;Description 1&quot; });
              ProductsList.Add(new ProductModel { ProductId = 1, ProductName = &quot;Name 1&quot;, ProductDescription = &quot;Description 1&quot; });
              ProductsList.Add(new ProductModel { ProductId = 1, ProductName = &quot;Name 1&quot;, ProductDescription = &quot;Description 1&quot; });
  
              ItemSelectedCommand = new AsyncCommand&lt;ProductModel&gt;(OnItemSelectedCommand);
          }//end of con
  
          public ProductModel previousItemSelected;
          public ProductModel currentItemSelected;
          public ProductModel ItemSelectedEvent
          {
              get =&gt; currentItemSelected;
              set
              {
                  currentItemSelected = value;
                  OnPropertyChanged();
              }
          }
          
          async Task OnItemSelectedCommand(ProductModel productModel)
          {
              if (productModel == null)
                  return;
  
               await Application.Current.MainPage.DisplayAlert(&quot;Selected works&quot;, &quot;yayyy&quot;, &quot;Cancel&quot;);
          }
          ...
  
  
  #region INotifyPropertyChanged
          public event PropertyChangedEventHandler PropertyChanged;
          protected void OnPropertyChanged([CallerMemberName] string propertyName = &quot;&quot;)
          {
              var changed = PropertyChanged;
              if (changed == null)
                  return;
  
              changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
          #endregion
      }
  }
  </code></pre>
  <p>** Output **</p>
  <pre><code>[Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_new_object_array'.
  [Mono] Probing 'java_interop_jnienv_new_object_array'.
  [Mono] Found as 'java_interop_jnienv_new_object_array'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_set_object_array_element'.
  [Mono] Probing 'java_interop_jnienv_set_object_array_element'.
  [Mono] Found as 'java_interop_jnienv_set_object_array_element'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_is_assignable_from'.
  [Mono] Probing 'java_interop_jnienv_is_assignable_from'.
  [Mono] Found as 'java_interop_jnienv_is_assignable_from'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_new_float_array'.
  [Mono] Probing 'java_interop_jnienv_new_float_array'.
  [Mono] Found as 'java_interop_jnienv_new_float_array'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_set_float_array_region'.
  [Mono] Probing 'java_interop_jnienv_set_float_array_region'.
  [Mono] Found as 'java_interop_jnienv_set_float_array_region'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_get_float_array_region'.
  [Mono] Probing 'java_interop_jnienv_get_float_array_region'.
  [Mono] Found as 'java_interop_jnienv_get_float_array_region'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_call_float_method_a'.
  [Mono] Probing 'java_interop_jnienv_call_float_method_a'.
  [Mono] Found as 'java_interop_jnienv_call_float_method_a'.
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_new_local_ref'.
  [Mono] Probing 'java_interop_jnienv_new_local_ref'.
  [Mono] Found as 'java_interop_jnienv_new_local_ref'.
  [Mono] Requesting loading reference 11 (of 12) of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.AndroidX.Fragment.dll
  [Mono] Loading reference 11 of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.AndroidX.Fragment.dll asmctx DEFAULT, looking for Xamarin.AndroidX.ViewPager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  [Mono] Assembly Ref addref Xamarin.AndroidX.Fragment[0x793c9b6900] -&gt; Xamarin.AndroidX.ViewPager[0x793c9f8300]: 3
  [Mono] Requesting loading reference 12 (of 15) of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.Google.Android.Material.dll
  [Mono] Loading reference 12 of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.Google.Android.Material.dll asmctx DEFAULT, looking for Xamarin.AndroidX.ViewPager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  [Mono] Assembly Ref addref Xamarin.Google.Android.Material[0x793c9f8b00] -&gt; Xamarin.AndroidX.ViewPager[0x793c9f8300]: 4
  [TabLayout] MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead
  [Mono] Requesting loading reference 4 (of 11) of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.CommunityToolkit.dll
  [Mono] Loading reference 4 of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.CommunityToolkit.dll asmctx DEFAULT, looking for System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
  [Mono] Assembly Ref addref Xamarin.CommunityToolkit[0x793c9f8500] -&gt; System[0x794c9eb480]: 9
  [Mono] Requesting loading reference 9 (of 11) of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.CommunityToolkit.dll
  [Mono] Loading reference 9 of /data/data/com.companyname.testapp02_mvvm_product/files/.__override__/Xamarin.CommunityToolkit.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
  [Mono] Assembly Ref addref Xamarin.CommunityToolkit[0x793c9f8500] -&gt; System.Core[0x793c887980]: 5
  [Mono] DllImport searching in: '__Internal' ('(null)').
  [Mono] Searching for 'java_interop_jnienv_throw'.
  [Mono] Probing 'java_interop_jnienv_throw'.
  [Mono] Found as 'java_interop_jnienv_throw'.
  **System.Reflection.TargetInvocationException:** 'Exception has been thrown by the target of an invocation.'
  </code></pre>