title: Xamarin - How to pass variable to viewmodel
body: <p>I have 2 Views. One has an event, that passes two variables to the second page and loads the page up:</p>
  
  <pre><code>private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          var item = (GalleryListEntry)e.CurrentSelection.FirstOrDefault();
          Navigation.PushModalAsync(new Gallery(item.PhotographerCode, item.GalleryCode));
      }
  </code></pre>
  
  <p>in the second page I have this:</p>
  
  <pre><code>public Gallery(string photographerCode, string galleryCode)
      {
          InitializeComponent();
      }
  </code></pre>
  
  <p>The second page, has a Collection view that has its own Bindingsource.
  For this bindingsource i have a Model, a Service, and a ViewModel. The service is called by the Viewmodel, and returns a List of images to be shown in the second page's collection view.</p>
  
  <p>Inside this service class, I would need to access to the two variables passed above (<code>photograperCode</code>and <code>galleryCode</code>) but I cannot figure out how to pass the variables to the ViewModel so i can then forward it to the class.</p>
  
  <p>ViewModel:</p>
  
  <pre><code>using GalShare.Model;
  using GalShare.Service;
  using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Text;
  
  namespace GalShare.ViewModel
  {
      class GalleryViewModel
      {
          public ObservableCollection&lt;Gallery&gt; Galleries { get; set; }
          public GalleryViewModel()
          {
          Galleries = new GalleryService().GetImageList();
          }
      }
  }
  </code></pre>
  
  <p>I tried like this</p>
  
  <pre><code>        ((GalleryViewModel)this.BindingContext).pCode = photographerCode;
          ((GalleryViewModel)this.BindingContext).gCode = galleryCode;
  </code></pre>
  
  <p>but I get this error: <code>System.NullReferenceException: 'Object reference not set to an instance of an object.'</code> 
  BindingContext is Null, but in the Xaml file i have this:  </p>
  
  <pre><code>&lt;ContentPage.BindingContext&gt;
      &lt;vm:GalleryViewModel/&gt;
  &lt;/ContentPage.BindingContext&gt;
  </code></pre>