title: ViewModel always returns null when trying to access it from code-behind
body: <p>I am still learning Xamarin.Forms, so please bear with me.</p>
  <p>I am developing a control, that let's you take pictures with your camera, displays them in a FlowListView and then ultimately I want to take these pictures, turn them into E-Mail attachments and send them off.</p>
  <p>While the Gallery part of the control is working and the E-Mail part was already written by somebody else, for some reason I can't access the control's ViewModel from my E-Mail Code-Behind.</p>
  <p>I wrote the control in MVVM, the E-Mail part is written in xaml with code-behind.</p>
  <p>Whenever I try to access the ImagePickerViewModel to get the contents of my AttachmentList, I get returned null.</p>
  <p>I have already tried not initializing the ImagePickerViewModel in the E-Mail code-behind, it still returns null.</p>
  <p>What would be the correct way to do this?</p>
  <p><em><strong>ImagePickerViewModel.cs</strong></em></p>
  <pre><code> [XamlCompilation(XamlCompilationOptions.Compile)]
      public class ImagePickerViewModel : BindableObject, INotifyPropertyChanged
      {
          // HERE WOULD BE A TON OF DEPENDENCIES
  
          public ImagePickerViewModel()
  
          {
              // Commands
              TakePictureCmd = new Command(TakePicture);
  
              //ObservableCollection
              TakenPicturesList = new ObservableCollection&lt;Model.ImagePicker&gt;();
              AttachmentList = new List&lt;BaseObject&gt;();   
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          public Model.ImagePicker NewPicture { get; set; }
  
          public BaseObject attachment { get; set; }
          public List&lt;BaseObject&gt; AttachmentList { get; set; }
  
          public ObservableCollection&lt;Model.ImagePicker&gt; TakenPicturesList { get; set; }
  
          public string selectedImage { get; set; }
          public string Path
          {
              get { return NewPicture.ImagePath; }
              set { NewPicture.ImagePath = value; OnPropertyChanged(nameof(Path)); }
          }
  
          public Command TakePictureCmd { get; set; }
  
  
          private async void TakePicture()
          {
              var path = await _cameraService.TakePictureAsync();
              TakenPicturesList.Add(new Model.ImagePicker { ImagePath = path });
              GetFilesForAttachment(path);
          }
  
  
          private void GetFilesForAttachement(string filePath)
          {
              attachment = Attachment.FromFile(filePath);
              if (attachment != null)
              {
                  AttachmentList.Add(attachment);
              }
          }
  
          public void RaisedOnPropertyChanged(string _PropertyName)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(_PropertyName));
          }
      }
  </code></pre>
  <p><em><strong>ImagePickerView.xaml</strong></em></p>
  <pre><code>&lt;ContentView xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
               xmlns:xct=&quot;http://xamarin.com/schemas/2020/toolkit&quot;
               xmlns:flv=&quot;clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView&quot;
               xmlns:ffimageloading=&quot;clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms&quot;
               xmlns:local=&quot;clr-namespace:SolIT.Mobile.Client&quot; 
               xmlns:vm=&quot;clr-namespace:SolIT.Mobile.Client.Features.Shared.Controls.ImagePicker.ViewModel&quot;
               xmlns:buttons=&quot;clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms&quot;
               x:Class=&quot;SolIT.Mobile.Client.Features.Shared.Controls.ImagePicker.View.ImagePickerView&quot;
               x:Name=&quot;View_ImagePickerView&quot;&gt;
      &lt;ContentView.BindingContext&gt;
          &lt;vm:ImagePickerViewModel ContextView=&quot;{x:Reference View_ImagePickerView}&quot;/&gt;
      &lt;/ContentView.BindingContext&gt;
  
      &lt;Frame&gt;
          &lt;ContentView.Content&gt;
              &lt;StackLayout&gt;
  
                  &lt;Grid 
            ColumnSpacing=&quot;0&quot;
            HorizontalOptions=&quot;CenterAndExpand&quot;&gt;
  
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width=&quot;Auto&quot;/&gt;
                          &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
                          &lt;RowDefinition Height=&quot;*&quot;/&gt;
                      &lt;/Grid.RowDefinitions&gt;
  
                      &lt;buttons:SfButton
              x:Name=&quot;Btn_takePicture&quot;
              Grid.Column=&quot;1&quot;
              Text=&quot;{local:Translate Common.SelectFilesPage.TakePicture}&quot;
              TextColor=&quot;Black&quot;
              ImageSource=&quot;{Binding TakePictureIcon}&quot;
              ShowIcon=&quot;true&quot;
              BackgroundColor=&quot;Transparent&quot;
              ImageAlignment=&quot;Top&quot;
              HorizontalOptions=&quot;FillAndExpand&quot;
              Margin=&quot;3&quot;
              CornerRadius=&quot;5&quot;
              Command=&quot;{Binding TakePictureCmd}&quot;/&gt;
                  &lt;/Grid&gt;
  
                  &lt;BoxView Grid.ColumnSpan=&quot;2&quot; Grid.Row=&quot;1&quot; HeightRequest=&quot;1&quot; BackgroundColor=&quot;{StaticResource Win10Devider}&quot;/&gt;
  
                  &lt;flv:FlowListView x:Name=&quot;pictureFlowListView&quot; SeparatorVisibility=&quot;None&quot; HasUnevenRows=&quot;true&quot;
              FlowColumnMinWidth=&quot;110&quot; FlowItemsSource=&quot;{Binding TakenPicturesList}&quot;&gt;
                                    &lt;!--FlowItemTappedCommand=&quot;{Binding PictureTappedCmd}&quot;
                                    FlowLastTappedItem=&quot;{Binding SelectedItem}&quot;--&gt;
                      &lt;flv:FlowListView.FlowColumnTemplate&gt;
                          &lt;DataTemplate&gt; 
                              &lt;Grid Padding=&quot;3&quot;&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition Height=&quot;*&quot; /&gt;
                                  &lt;/Grid.RowDefinitions&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition Width=&quot;*&quot; /&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
                                  &lt;StackLayout&gt;
                                  &lt;ffimageloading:CachedImage HeightRequest=&quot;100&quot; Aspect=&quot;AspectFill&quot;
                              DownsampleHeight=&quot;100&quot; DownsampleUseDipUnits=&quot;false&quot; x:Name=&quot;imageCached&quot;
                              Source=&quot;{Binding ImagePath}&quot;&gt;
                                      &lt;ffimageloading:CachedImage.GestureRecognizers&gt;
                                          &lt;TapGestureRecognizer Command=&quot;{Binding Path=BindingContext.PictureTappedCmd, Source={x:Reference pictureFlowListView}}&quot; CommandParameter=&quot;{x:Reference imageCached}&quot;&gt;&lt;/TapGestureRecognizer&gt;
                                      &lt;/ffimageloading:CachedImage.GestureRecognizers&gt;
                                  &lt;/ffimageloading:CachedImage&gt;
                                  &lt;/StackLayout&gt;
                              &lt;/Grid&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/flv:FlowListView.FlowColumnTemplate&gt;
                  &lt;/flv:FlowListView&gt;
              &lt;/StackLayout&gt;
          &lt;/ContentView.Content&gt;
      &lt;/Frame&gt;
  &lt;/ContentView&gt;
  </code></pre>
  <p><em><strong>EmailCreatePage.xaml.cs</strong></em></p>
  <pre><code> [XamlCompilation(XamlCompilationOptions.Compile)]
      public partial class EmailCreatePage: ContentPage
      {
          // HERE WOULD BE A TON OF DEPENDENCIES
  
          public EmailCreateViewModel ViewModel { get; }
          public ImagePickerViewModel ViewModelImagePicker {get;}
  
          public EmailCreatePage()
          {
              InitializeComponent();
              BindingContext = ViewModel = new EmailCreateViewModel();
              ViewModelImagePicker = new ImagePickerViewModel();
          }
  private void CreateAttachment()
          {
   
                  var attachmentList = GetFileList();
                  foreach (var file in attachmentList)
                  {
                      var attachment = Attachment.FromFile(file.ToString());
                      if (attachment != null)
                      {
  
                          ViewModel.CubesMessage.ConnectedObjects.Add(attachment);
                      }
              }
  
  public List&lt;BaseObject&gt; GetFileList()
          {
  // GET LIST FROM OTHER VIEWMODEL HERE
  // VIEWMODEL IS ALWAYS NULL
            var attachmentList = ImagePickerViewModel.AttachmentList;
            return attachmentList;
  }
  
  // HERE WOULD BE THE CODE THAT SENDS THE EMAIL OFF AFTER A BUTTON CLICK
  </code></pre>