Sharing Prism Viewmodels WPF and Xamarin Forms

Viewed 57

Does anyone know if it is possible to share viewmodels between a WPF and Xamarin Forms app? Below is a snippet of a Xamarin Forms viewmodel. There are some interfaces that are unique to the Forms app such as the IPageDialogService and also the way navigation is handled so it doesn't seem as though we can do this but wanted to check.

         using System;
         using System.Threading.Tasks;
         using Prism.Commands;
         using Prism.Mvvm;
         using Prism.Navigation;
         using Prism.Services;

         namespace PrismSample.ViewModels
         {
               public class MainPageViewModel : BindableBase, IInitialize, IConfirmNavigation, 
                        INavigationAware
               {
                 private INavigationService _navigationService { get; }
                 private IPageDialogService _dialogs { get; }

                 public MainPageViewModel(INavigationService navigationService, IPageDialogService 
                           dialogs)
                 {
                      _navigationService = navigationService;
                      _dialogs = dialogs;

                       NavigateCommand = new DelegateCommand<string>(OnNavigateCommandExecuted);
                  }
         }
1 Answers

If your code contains references to any type in Prism.Forms or Prism.Wpf you cannot share it between platforms.

Shared libraries should reference Prism.Core only. You could then create platform specific libraries that references your shared Prism.Core base library and adds the platform specific functionality for Xamarin.Forms and WPF respectively.

Related