title: Switching through different Pages in MVVM
body: <p>I am trying to implement the ability to switch through different Pages in my Xamarin MVVM project. I have three folders - "Models", "Views" and "ViewModels". "Views" contains "MainView" which's role is to display other Views. </p>
  
  <p><strong>MainView.xaml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               x:Class="KeepFit.Views.MainView"&gt;
  
    &lt;ContentPresenter x:Name="ContentArea" Content="{Binding CurrentView}"/&gt;
  
  &lt;/ContentPage&gt; 
  </code></pre>
  
  <p><strong>MainView.xaml.cs:</strong></p>
  
  <pre><code>public partial class MainView : ContentPage
  {
      public MainView()
      {
          InitializeComponent();
  
          BindingContext = new MainViewModel();
      }
  }
  </code></pre>
  
  <p>As you can see, "MainView" is binded to "MainViewModel" class.</p>
  
  <p><strong>MainViewModel.cs:</strong></p>
  
  <pre><code>public class MainViewModel : INotifyPropertyChanged
      {
          public Command SwitchViewsCommand { get; private set; }
  
          public MainViewModel()
          {
              SwitchViewsCommand = new Command((parameter) =&gt;
              CurrentView = (ContentPage)Activator.CreateInstance(parameter as Type)); 
              CurrentView = new HomeView();
          }
  
          private ContentPage _currentView;
          public ContentPage CurrentView
          {
              get
              {
                  return _currentView;
              }
              set
              {
                  if (value != _currentView)
                  {
                      _currentView = value;
                      OnPropertyChanged("CurrentView");
                  }
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected virtual void OnPropertyChanged([CallerMemberName]
          string propertyName = null)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  </code></pre>
  
  <p>The CurrentView is set to my HomeView (let us say it is default ContentPage) in the constructor. It is binded to the ContentPresenter, so it should be visible at the beginning of my application's runtime. But it is not.</p>
  
  <p>I have noticed that the ContentPresenter is expecting the Xamarin.Forms.View object as the "Content" - and my Views are inheriting from Xamarin.Forms.ContentPage. But if I change them to ContentViews for example - I will not be able to set the BindingContext for them. Could anybody explain what am I doing wrong?</p>