title: How to create a separate view model for a contentview in Xamarin forms having prism?
body: <p>Problem Statement
  I want to create a contentview user control having its own view model that can be used in multiple content pages. </p>
  
  <p>Issue in below implementation
  I have extended my App.xaml.cs as mentioned below. But once the navigation works from the contentpage having contentview user control but if I navigate again to that page, the navigation doesn't work. Just to add to it, view.Parent also comes out to be null in the below code. </p>
  
  <p>Please help.</p>
  
  <pre><code>using OEP.Views;
  using Prism;
  using Prism.Common;
  using Prism.Ioc;
  using Prism.Mvvm;
  using Prism.Navigation;
  using Prism.Unity;
  using Unity.Resolution;
  using Xamarin.Forms;
  using Xamarin.Forms.Xaml;
  
  [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
  namespace OEP
  {
      public partial class App : PrismApplication
      {
          public App() : this(null) { }
  
          public App(IPlatformInitializer initializer) : base(initializer) { }
  
          protected override async void OnInitialized()
          {
              InitializeComponent();
              //await NavigationService.NavigateAsync("NewOrderPage");
              await NavigationService.NavigateAsync("LoginPage");
              //await NavigationService.NavigateAsync("HomePage");
          }
  
          protected override void RegisterTypes(IContainerRegistry containerRegistry)
          {
              containerRegistry.RegisterForNavigation&lt;LoginPage&gt;();
              containerRegistry.RegisterForNavigation&lt;ForgotPasswordPage&gt;();
              containerRegistry.RegisterForNavigation&lt;HomePage&gt;();
              containerRegistry.RegisterForNavigation&lt;CustomerDetailsPage&gt;();
              containerRegistry.RegisterForNavigation&lt;NewOrderPage&gt;();
              //Container.Resolve&lt;HomePageCustomersUserControl&gt;("Customers");
              //containerRegistry.Register&lt;HomePageCustomersUserControl, HomePageCustomersUserControlViewModel&gt;();
              //ViewModelLocationProvider.Register&lt;HomePageCustomersUserControl&gt;(() =&gt; Container.Resolve&lt;HomePageCustomersUserControlViewModel&gt;());
          }
  
          protected override void ConfigureViewModelLocator()
          {
              ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =&gt;
              {
                  Page page = null;
                  switch (view)
                  {
                      case Page page1:
                          page = page1;
                          break;
                      case Element customView:
                          page = GetPageFromElement(customView);
                          // Existing parameter with the Page
                          break;
                  }
  
                  var navService = CreateNavigationService(page);
                  ParameterOverrides overrides = new ParameterOverrides
                  {
                          { "navigationService", navService }
                  };
                  return Container.GetContainer().Resolve(type, type.GetType().Name, overrides);
  
              });
          }
  
          // Currently exists
          protected INavigationService CreateNavigationService(Page page)
          {
              var navigationService = NavigationService;
              ((IPageAware)navigationService).Page = page;
              return navigationService;
          }
  
          protected INavigationService CreateNavigationService(object view)
          {
              switch (view)
              {
                  case Page page:
                      return CreateNavigationService(page);
                  case Element element:
                      var parentPage = GetPageFromElement(element);
                      if (parentPage == null)
                      {
                          return null;
                      }
                      return CreateNavigationService(parentPage);
                  default:
                      return null;
              }
          }
  
          private Page GetPageFromElement(Element view)
          {
              switch (view.Parent)
              {
                  case Page page:
                      return page;
                  case null:
                      return null;
                  default:
                      return GetPageFromElement(view.Parent);
              }
          }
  
          protected override void OnStart()
          {
              // Handle when your app starts
          }
  
          protected override void OnSleep()
          {
              // Handle when your app sleeps
          }
  
          protected override void OnResume()
          {
              // Handle when your app resumes
          }
      }
  }
  </code></pre>