How to create a separate view model for a contentview in Xamarin forms having prism?












0















Problem Statement
I want to create a contentview user control having its own view model that can be used in multiple content pages.



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.



Please help.



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<LoginPage>();
containerRegistry.RegisterForNavigation<ForgotPasswordPage>();
containerRegistry.RegisterForNavigation<HomePage>();
containerRegistry.RegisterForNavigation<CustomerDetailsPage>();
containerRegistry.RegisterForNavigation<NewOrderPage>();
//Container.Resolve<HomePageCustomersUserControl>("Customers");
//containerRegistry.Register<HomePageCustomersUserControl, HomePageCustomersUserControlViewModel>();
//ViewModelLocationProvider.Register<HomePageCustomersUserControl>(() => Container.Resolve<HomePageCustomersUserControlViewModel>());
}

protected override void ConfigureViewModelLocator()
{
ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =>
{
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
}
}
}









share|improve this question



























    0















    Problem Statement
    I want to create a contentview user control having its own view model that can be used in multiple content pages.



    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.



    Please help.



    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<LoginPage>();
    containerRegistry.RegisterForNavigation<ForgotPasswordPage>();
    containerRegistry.RegisterForNavigation<HomePage>();
    containerRegistry.RegisterForNavigation<CustomerDetailsPage>();
    containerRegistry.RegisterForNavigation<NewOrderPage>();
    //Container.Resolve<HomePageCustomersUserControl>("Customers");
    //containerRegistry.Register<HomePageCustomersUserControl, HomePageCustomersUserControlViewModel>();
    //ViewModelLocationProvider.Register<HomePageCustomersUserControl>(() => Container.Resolve<HomePageCustomersUserControlViewModel>());
    }

    protected override void ConfigureViewModelLocator()
    {
    ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =>
    {
    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
    }
    }
    }









    share|improve this question

























      0












      0








      0








      Problem Statement
      I want to create a contentview user control having its own view model that can be used in multiple content pages.



      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.



      Please help.



      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<LoginPage>();
      containerRegistry.RegisterForNavigation<ForgotPasswordPage>();
      containerRegistry.RegisterForNavigation<HomePage>();
      containerRegistry.RegisterForNavigation<CustomerDetailsPage>();
      containerRegistry.RegisterForNavigation<NewOrderPage>();
      //Container.Resolve<HomePageCustomersUserControl>("Customers");
      //containerRegistry.Register<HomePageCustomersUserControl, HomePageCustomersUserControlViewModel>();
      //ViewModelLocationProvider.Register<HomePageCustomersUserControl>(() => Container.Resolve<HomePageCustomersUserControlViewModel>());
      }

      protected override void ConfigureViewModelLocator()
      {
      ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =>
      {
      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
      }
      }
      }









      share|improve this question














      Problem Statement
      I want to create a contentview user control having its own view model that can be used in multiple content pages.



      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.



      Please help.



      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<LoginPage>();
      containerRegistry.RegisterForNavigation<ForgotPasswordPage>();
      containerRegistry.RegisterForNavigation<HomePage>();
      containerRegistry.RegisterForNavigation<CustomerDetailsPage>();
      containerRegistry.RegisterForNavigation<NewOrderPage>();
      //Container.Resolve<HomePageCustomersUserControl>("Customers");
      //containerRegistry.Register<HomePageCustomersUserControl, HomePageCustomersUserControlViewModel>();
      //ViewModelLocationProvider.Register<HomePageCustomersUserControl>(() => Container.Resolve<HomePageCustomersUserControlViewModel>());
      }

      protected override void ConfigureViewModelLocator()
      {
      ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =>
      {
      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
      }
      }
      }






      xamarin.forms prism






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 13:14









      VantageVantage

      257




      257
























          1 Answer
          1






          active

          oldest

          votes


















          1














          This is supported in Prism 7.1. The following is taken directly from the Prism Unit Tests. If you're following the naming convention you actually do not need to register anything, you simply need to set ViewModelLocator.AutowirePartialView with a reference to the parent page.



          <ContentView
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.PartialView">
          <StackLayout>
          <Label Text="{Binding SomeText}" />
          <Button Command="{Binding NavigateCommand}"
          x:Name="navigateButton" />
          </StackLayout>
          </ContentView>

          <ContentPage
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Prism.DI.Forms.Tests.Mocks.Views"
          xmlns:prism="clr-namespace:Prism.Ioc;assembly=Prism.Forms"
          xmlns:mvvm="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
          xmlns:converters="using:Prism.Forms.Tests.Mocks.Converters"
          Title="{Binding Title}"
          x:Name="xamlViewMock"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMock">
          <ContentPage.Resources>
          <ResourceDictionary>
          <prism:ContainerProvider x:TypeArguments="converters:MockValueConverter" x:Key="mockValueConverter" />
          </ResourceDictionary>
          </ContentPage.Resources>
          <StackLayout>
          <local:PartialView mvvm:ViewModelLocator.AutowirePartialView="{x:Reference xamlViewMock}" />
          <Entry x:Name="testEntry"
          Text="{Binding Test,Converter={StaticResource mockValueConverter}}" />
          </StackLayout>

          </ContentPage>



          • Partial View

          • Consuming Page


          In the event you need to follow some custom Naming scheme you simply need to call:



          ViewModelLocationProvider.Register<MyView, SomeViewModel>();





          share|improve this answer
























          • It worked. Thanks.

            – Vantage
            Nov 21 '18 at 11:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393821%2fhow-to-create-a-separate-view-model-for-a-contentview-in-xamarin-forms-having-pr%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This is supported in Prism 7.1. The following is taken directly from the Prism Unit Tests. If you're following the naming convention you actually do not need to register anything, you simply need to set ViewModelLocator.AutowirePartialView with a reference to the parent page.



          <ContentView
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.PartialView">
          <StackLayout>
          <Label Text="{Binding SomeText}" />
          <Button Command="{Binding NavigateCommand}"
          x:Name="navigateButton" />
          </StackLayout>
          </ContentView>

          <ContentPage
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Prism.DI.Forms.Tests.Mocks.Views"
          xmlns:prism="clr-namespace:Prism.Ioc;assembly=Prism.Forms"
          xmlns:mvvm="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
          xmlns:converters="using:Prism.Forms.Tests.Mocks.Converters"
          Title="{Binding Title}"
          x:Name="xamlViewMock"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMock">
          <ContentPage.Resources>
          <ResourceDictionary>
          <prism:ContainerProvider x:TypeArguments="converters:MockValueConverter" x:Key="mockValueConverter" />
          </ResourceDictionary>
          </ContentPage.Resources>
          <StackLayout>
          <local:PartialView mvvm:ViewModelLocator.AutowirePartialView="{x:Reference xamlViewMock}" />
          <Entry x:Name="testEntry"
          Text="{Binding Test,Converter={StaticResource mockValueConverter}}" />
          </StackLayout>

          </ContentPage>



          • Partial View

          • Consuming Page


          In the event you need to follow some custom Naming scheme you simply need to call:



          ViewModelLocationProvider.Register<MyView, SomeViewModel>();





          share|improve this answer
























          • It worked. Thanks.

            – Vantage
            Nov 21 '18 at 11:34
















          1














          This is supported in Prism 7.1. The following is taken directly from the Prism Unit Tests. If you're following the naming convention you actually do not need to register anything, you simply need to set ViewModelLocator.AutowirePartialView with a reference to the parent page.



          <ContentView
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.PartialView">
          <StackLayout>
          <Label Text="{Binding SomeText}" />
          <Button Command="{Binding NavigateCommand}"
          x:Name="navigateButton" />
          </StackLayout>
          </ContentView>

          <ContentPage
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Prism.DI.Forms.Tests.Mocks.Views"
          xmlns:prism="clr-namespace:Prism.Ioc;assembly=Prism.Forms"
          xmlns:mvvm="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
          xmlns:converters="using:Prism.Forms.Tests.Mocks.Converters"
          Title="{Binding Title}"
          x:Name="xamlViewMock"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMock">
          <ContentPage.Resources>
          <ResourceDictionary>
          <prism:ContainerProvider x:TypeArguments="converters:MockValueConverter" x:Key="mockValueConverter" />
          </ResourceDictionary>
          </ContentPage.Resources>
          <StackLayout>
          <local:PartialView mvvm:ViewModelLocator.AutowirePartialView="{x:Reference xamlViewMock}" />
          <Entry x:Name="testEntry"
          Text="{Binding Test,Converter={StaticResource mockValueConverter}}" />
          </StackLayout>

          </ContentPage>



          • Partial View

          • Consuming Page


          In the event you need to follow some custom Naming scheme you simply need to call:



          ViewModelLocationProvider.Register<MyView, SomeViewModel>();





          share|improve this answer
























          • It worked. Thanks.

            – Vantage
            Nov 21 '18 at 11:34














          1












          1








          1







          This is supported in Prism 7.1. The following is taken directly from the Prism Unit Tests. If you're following the naming convention you actually do not need to register anything, you simply need to set ViewModelLocator.AutowirePartialView with a reference to the parent page.



          <ContentView
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.PartialView">
          <StackLayout>
          <Label Text="{Binding SomeText}" />
          <Button Command="{Binding NavigateCommand}"
          x:Name="navigateButton" />
          </StackLayout>
          </ContentView>

          <ContentPage
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Prism.DI.Forms.Tests.Mocks.Views"
          xmlns:prism="clr-namespace:Prism.Ioc;assembly=Prism.Forms"
          xmlns:mvvm="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
          xmlns:converters="using:Prism.Forms.Tests.Mocks.Converters"
          Title="{Binding Title}"
          x:Name="xamlViewMock"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMock">
          <ContentPage.Resources>
          <ResourceDictionary>
          <prism:ContainerProvider x:TypeArguments="converters:MockValueConverter" x:Key="mockValueConverter" />
          </ResourceDictionary>
          </ContentPage.Resources>
          <StackLayout>
          <local:PartialView mvvm:ViewModelLocator.AutowirePartialView="{x:Reference xamlViewMock}" />
          <Entry x:Name="testEntry"
          Text="{Binding Test,Converter={StaticResource mockValueConverter}}" />
          </StackLayout>

          </ContentPage>



          • Partial View

          • Consuming Page


          In the event you need to follow some custom Naming scheme you simply need to call:



          ViewModelLocationProvider.Register<MyView, SomeViewModel>();





          share|improve this answer













          This is supported in Prism 7.1. The following is taken directly from the Prism Unit Tests. If you're following the naming convention you actually do not need to register anything, you simply need to set ViewModelLocator.AutowirePartialView with a reference to the parent page.



          <ContentView
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.PartialView">
          <StackLayout>
          <Label Text="{Binding SomeText}" />
          <Button Command="{Binding NavigateCommand}"
          x:Name="navigateButton" />
          </StackLayout>
          </ContentView>

          <ContentPage
          xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:local="clr-namespace:Prism.DI.Forms.Tests.Mocks.Views"
          xmlns:prism="clr-namespace:Prism.Ioc;assembly=Prism.Forms"
          xmlns:mvvm="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
          xmlns:converters="using:Prism.Forms.Tests.Mocks.Converters"
          Title="{Binding Title}"
          x:Name="xamlViewMock"
          x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMock">
          <ContentPage.Resources>
          <ResourceDictionary>
          <prism:ContainerProvider x:TypeArguments="converters:MockValueConverter" x:Key="mockValueConverter" />
          </ResourceDictionary>
          </ContentPage.Resources>
          <StackLayout>
          <local:PartialView mvvm:ViewModelLocator.AutowirePartialView="{x:Reference xamlViewMock}" />
          <Entry x:Name="testEntry"
          Text="{Binding Test,Converter={StaticResource mockValueConverter}}" />
          </StackLayout>

          </ContentPage>



          • Partial View

          • Consuming Page


          In the event you need to follow some custom Naming scheme you simply need to call:



          ViewModelLocationProvider.Register<MyView, SomeViewModel>();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 22:34









          Dan S.Dan S.

          3,1312920




          3,1312920













          • It worked. Thanks.

            – Vantage
            Nov 21 '18 at 11:34



















          • It worked. Thanks.

            – Vantage
            Nov 21 '18 at 11:34

















          It worked. Thanks.

          – Vantage
          Nov 21 '18 at 11:34





          It worked. Thanks.

          – Vantage
          Nov 21 '18 at 11:34




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393821%2fhow-to-create-a-separate-view-model-for-a-contentview-in-xamarin-forms-having-pr%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()