1

When I create a new Xamarin.Forms application using the Prism template pack the project gets created with a MainPageViewModel that inherits from ViewModelBase

Later I create and an additional View and ViewModel say for ChatPage. This ChatPageViewModelinherits from BindableBasenot ViewModelBase as generated by the Add New dialog.

I'd like to use ViewModelBase in all my View(Models) ViewModelBase inherits from ViewModelBase : BindableBase, INavigationAware, IDestructible

I try and change the new ChatPageViewModel : BindableBase to ChatPageViewModel : ViewModelBase but the constructor gets a red squiggly error; Error CS7036 There is no argument given that corresponds to the required formal parameter 'navigationService' of 'ViewModelBase.ViewModelBase(INavigationService)'

I see in App.xaml.cs that containerRegistry.RegisterForNavigation<NavigationPage>(); is implemented differently than the other pages containerRegistry.RegisterForNavigation<ChatPage, ChatPageViewModel>();

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterForNavigation<NavigationPage>();
    containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
    containerRegistry.RegisterForNavigation<SettingsPage, SettingsPageViewModel>();
    containerRegistry.RegisterForNavigation<ChatPage, ChatPageViewModel>();

    containerRegistry.RegisterSingleton<IXpdSettings, XpdSettings>();
    containerRegistry.RegisterSingleton<IMqttDataService, MqttDataService>();
}

Is there a way I can inherit from ViewModelBase? Could / should it be implemented in the XamarinForms Prism templating?

Jeff Albrecht
  • 3,723
  • 1
  • 17
  • 22
  • So you want to create a Template in Visual studio that makes `ViewModelBase` as the base class View models your create? – FreakyAli Apr 04 '19 at 20:28

1 Answers1

0

The answer is contained in my question. See the syntax that the MainPageViewModel (that was created by the initial project creation dialog) uses - where MainPageViewModel inherits from ViewModelBase unlike subsequent pages created with Add New dialog that inherit from BindableBase. For instance ChatPageViewModel inheriting from ViewModelBase rather than BindableBase.

public class ChatPageViewModel : ViewModelBase 
{
    private IXpdSettings _xpdsettings;
    public ChatPageViewModel(INavigationService navigationService, IXpdSettings xpdSettings)
        : base(navigationService)
    {
        Title = "Mqtt Chat";
        _xpdsettings = xpdSettings;
    }
}
Jeff Albrecht
  • 3,723
  • 1
  • 17
  • 22