How to navigate between pages using Prism in Xamarin.Forms? - c#

I'm trying to inject INavigationService into my ViewModel, so I can navigate between pages, but I don't know how to register ViewModel with parameter. Here's my App:
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
x:Class="PrismDemo.App" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<prism:PrismApplication.Resources>
<ResourceDictionary>
</ResourceDictionary>
</prism:PrismApplication.Resources>
</prism:PrismApplication>
and...
public partial class App
{
INavigationService _navigationService;
public App(IPlatformInitializer initializer = null) : base(initializer) { }
protected override void OnInitialized()
{
InitializeComponent();
_navigationService = NavigationService;
NavigationService.NavigateAsync("MainNavigationPage/Start");
}
protected override void RegisterTypes()
{
Container.RegisterTypeForNavigation<MainNavigationPage>();
Container.RegisterTypeForNavigation<StartPage, StartPageViewModel>("Start");
Container.RegisterTypeForNavigation<MiddlePage, MiddlePageViewModel>("Middle");
Container.RegisterTypeForNavigation<LastPage, LastPageViewModel>("Last");
}
}
How can I inject _navigationService into ViewModels?
Thanks for help

You don't actually have to register the ViewModel directly as long as you have followed the convention {MyProjectName}.Views.{MyPage} and {MyProjectName}.ViewModels.{MyPageViewModel}
To use INavigationService in the ViewModel simply add it to the Constructor. Remember it is a named service so it must be named navigationService. You can check out several examples in the Samples Repo.
public class MyPageViewModel
{
INavigationService _navigationService { get; }
public MyPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
NavigateCommand = new DelegateCommand<string>(OnNavigateCommandExecuted);
}
public DelegateCommand<string> NavigateCommand { get; }
public async void OnNavigateCommandExecuted(string path) =>
await _navigationService.NavigateAsync(path);
}

If you are using Prism, than you should inherit your App from PrismApplication:
instead of public partial class App should be public partial class App : PrismApplication.
No need to declare INavigationService _navigationService; in your App class, because it's already declared in PrismApplication as NavigationService property.
Prism uses IUnityContainer as Dependency Injection framework, which means for you, that all, that you will register in container:
Container.RegisterType<ISomeService, SomeServiceImplementation>();
Unity container will automatically inject in constructor:
Define a constructor in the target class that takes as a parameter the concrete type of the dependent class. The Unity container will instantiate and inject an instance.
PrismApplication registers INavigationService in a container, so, as I understand, you don't need to register your own instance and you could just add parameter of type INavigationService in constructor of view model and unity container will inject instance of registered type. The important thing, that you should give an exactly the navigationService name for your parameter, because Prism "expects" such parameter name for navigation service.

Related

Constructor Injection in Xamarin Froms .xaml.cs file classes

I want to take advantage of dependency injection in my Xamarin project but can't get constructor injection to work in C# classes behind XAML views. Is there any way to do it ?
I've seen guides how to setup dependency injections in View Models, to later use them as repositories but that doesn't work for me.
So far I tried Ninject and Unity.
Code:
This is the service I want to use inside of my PCL project:
public class MyService : IMyService
{
public void Add(string myNote)
{
//Add Note logic
}
}
Interface:
public interface IMyService
{
void Add(string myNote);
}
Unity setup in App.Xaml:
public App ()
{
InitializeComponent();
var unityContainer = new UnityContainer();
unityContainer.RegisterType<IMyService, MyService>();
var unityServiceLocator = new UnityServiceLocator(unityContainer);
ServiceLocator.SetLocatorProvider(() => unityServiceLocator);
MainPage = new MainMasterMenu(); //<-- feel that I'm missing something here as I shouldn't be creating class instances with DI, right ?
}
Usage that I'd like to see. This is .CS file behind a XAML starting page:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainMasterMenu : MasterDetailPage
{
private IMyService _myService;
public MainMasterMenu(IMyService myService)
{
_myService = myService
}
private void SomeFormControlClickEvent(object sender, ItemChangedEventArgs e)
{
_myService.Add("hi");
}
}
For that simple example creating the MainMasterMenu directly would be no issue, but you would have to pass the reference to your service
MainPage = new MainMasterMenu(unityContainer.Resolve<IMyService>());
But this would mean that you'll have to change that line every time the constructor of MainMasterMenu changes. You could circumvent this by registering the MainMasterMenu, too.
unityContainer.RegisterType<MainMasterMenu>();
...
MainPage = unityContainer.Resolve<MainMasterPage>();
Anyway, anytime you want to navigate to another page, which needs any dependency registered with unity, you'll have to make sure to resolve its dependencies properly, which requires (at least indirect) access to the unity container. You could pass a wrapper that encapsules the access to unity
interface IPageResolver
{
T ResolvePage<T>()
where T : Page;
}
and then implement that resolver with unity
public class UnityPageResolver
{
private IUnityContainer unityContainer;
public UnityPageResolver(IUnityContainer unityContainer)
{
this.unityContainer = unityContainer;
}
public T ResolvePage<T>()
where T : Page // do we need this restriction here?
{
return unityContainer.Resolve<T>();
}
}
This gets registered with unity
unityContainer.RegisterInstance<IUnityContainer>(this);
unityContainer.RegisterType<IPageResolver, UnityPageResolver>();
But you should have a look at the Prism library (see here) that solves many of the issues (e.g. it provides an INavigationService that lets you navigate to other pages without caring about the dependencies and it provides facilities to resolve viewmodels automatically, including dependencies).

How is this viewModel being created?

I have a simple application here but I am unsure of how my ViewModel is being created. I am assuming it's from the unity container but I am unsure and curious. The module initializes and registers the view with the region. The view's code behind has the ViewModel initialized in it's constructor and the ViewModel calls some services that were previously registered.
My question is how is the ViewModel created in the View's code behind when I've never registered the ViewModel type with the unity container? Is there some magic happening in the RegisterViewWithRegion method?
AlarmsModule.cs: This simply registers the view with the region
[Module(ModuleName = "AlarmsModule")]
public class AlarmsModule : IModule
{
[Dependency]
public IRegionManager regionManager { get; set; }
public void Initialize()
{
regionManager.RegisterViewWithRegion("AlarmsRegion", typeof(AlarmPanel.View));
}
}
View.xaml.cs:
public partial class View : UserControl
{
public View(ViewModel vm)
{
InitializeComponent();
DataContext = vm;
}
}
ViewModel.cs
public class ViewModel: DependencyObject
{
IEventAggregator _eventAggregator;
public ObservableCollection<IAlarmContainer> AlarmList { get; set; }
IAlarmService _alarmService;
public ViewModel(IAlarmService alarmService)
{
//Adding an alarm from the alarm service, which is injected into this viewModel
AlarmList = alarmService.AlarmList;
}
}
The view model is created by the unity container in the DoGetInstance method of the UnityServiceLocatorAdapter class in the Prism.Unity assembly which is in turn called by the RegisterViewWithRegion method through some other methods of the RegionViewRegistry class.
Unity is able to resolve the view model type automatically provided that it has a default parameterless constructor.
You could verify this yourself using the following code:
var view = unityContainer.Resolve(typeof(View), null); //will automatically resolve the view model type and inject the view with an instance of it

How to get the same instance of object from Unity container in various modules of Prism application?

I have a Prism application where I have three modules:
SharedServiceModule(I am using SharedServices to communicate between modules)
ModuleA
ModuleB
SharedServiceModule just has interface and its implementation CommonService:
public interface ICommonService
{
string SomeStorage { get; set; }
}
public class CommonService : ICommonService
{
string fooStorage;
public string FooStorage
{
get
{
return fooStorage;
}
set
{
fooStorage = value;
OnPropertyChanged("FooStorage");
}
}
}
What I want is to create communication between modules using Shared Services. So I assign «ModuleAValue» at ViewModelA of ModuleA and then I want to read this value in ViewModelB of ModuleB. Let's see details.
I create a single instance of ICommonService in ViewModelA of ModuleA and assign a value "ModuleAValue" to FooStorage:
Method of ViewModelA:
unityContainer = new UnityContainer();
unityContainer.RegisterType<ICommonService, CommonService>(new ContainerControlledLifetimeManager());
IMyService someFoo = unityContainer.Resolve<ICommonService>();
someFoo.FooStorage = "ModuleAValue";//value is "ModuleAValue" in FooStorage
Then I want to read this data in viewModelB of ModuleB. But value of FooStorage is not 'Module A', but just empty value:
Method of ViewModelB:
IUnityContainer unityContainer=new UnityContainer//creation of UnityContainer in ModuleB
ICommonService someFoo = unityContainer.Resolve<CommonService>();
string str=someFoo.FooStorage;//value is empty in
FooStorage, but it should be "ModuleAValue"
My Bootstrapper is:
public class Bootstrapper:UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType<IShellViewModel, ShellViewModel>();
RegisterTypeIfMissing(typeof(IMyService), typeof(MyService), true);
}
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
mappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
return mappings;
}
protected override IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleAModule));
catalog.AddModule(typeof(ModuleBModule));
return catalog;
}
}
What am I doing wrong? In my view, Unity always creates new instance of CommonService. What am I doing wrong while taking the same instance from Unity container?
Any help will be greatly appreciated!
Your app's bootstrapper creates an UnityContainer for you, see UnityBootstrapper.cs:
protected virtual IUnityContainer CreateContainer()
{
return new UnityContainer();
}
The bootstrapper should also register the container as viewmodel factory:
protected override void ConfigureContainer()
{
base.ConfigureContainer();
ViewModelLocationProvider.SetDefaultViewModelFactory( type => Container.Resolve( type ) );
}
In your module definition class, you can have this 'global' container injected as dependency:
public class ClientModule : IModule
{
private readonly IUnityContainer _unityContainer;
public ClientModule( IUnityContainer unityContainer )
{
_unityContainer = unityContainer;
}
}
In your module's initialization, you register types or instances with this container:
public void Initialize()
{
// Register services
_unityContainer.RegisterType<IGameClock, LocalGameClock>( new ContainerControlledLifetimeManager() );
_unityContainer.RegisterType<IWorldStateService, LocalWorldStateService>( new ContainerControlledLifetimeManager() );
}
In your views (in the xaml), you can now use ViewModelLocator.AutoWireViewModel="True" to automatically create viewmodels for your views. The ViewModelLocationProvider will use the 'global' container to resolve the viewmodels (as defined above), so all viewmodels will receive the same instance of our, say, IGameClock.
Ancilliary piece of advice: you don't want to call Resolve directly yourself. Using it this way defeats the whole purpose of using unity in the first place. It's better to have your dependencies injected as constructor parameters, and to only use the container at the highest level, i.e. in module initialization. And you should never need to create more than one container, lest you know exactly what you're doing.
You shouldn't create the new container instance. Usually there should be only one container instance in your application.
If you are using prism your view model should be created from a container also (if prism is responsible for view model creation than it is created from container ). In such case just create the constructor in your view model with parameter of type ICommonService like this:
public ViewModelA(ICommonService service) { ... }
then during creation of the ViewModel the same instance of the service will be injected to that ViewModel.
And usually common services are registered in the Shell during application startup. But if you want you can also register a service in a module, just use the same unity container that was created during application start. In the viewmodel use constructor with parameter of type IUnityContainer.

Prism for Xamarin, dependency failed for INavigationService

I am working on a sample Xamarin.Forms App for iOS, using PRISM.
But when I tried to use INavigationService in ViewModel, it is failed saying dependency failed for interface INavigationService
I dont know what wrong I am doing, how can we get around it or is it a bug in INavigationServices injection.
Thanks
Divikiran Ravela
Heres the code
public App()
{
// The root page of your application
Prism = new PrismBootStrap();
Prism.Run(this);
}
public class PrismBootStrap : UnityBootstrapper
{
protected override Page CreateMainPage()
{
return new NavigationPage(Container.Resolve<HomePage>());
}
protected override void RegisterTypes()
{
Container.RegisterTypeForNavigation<HomePage, HomePageViewModel>();
}
}
public class HomePage : ContentPage
{
public HomePageViewModel ViewModel { get; set; }
public HomePage()
{
ViewModel = App.Prism.Container.Resolve<HomePageViewModel>();
Content = new StackLayout
{
Children = {
new Label { Text = "Hello ContentPage" }
}
};
}
}
public class HomePageViewModel : BindableBase
{
private readonly INavigationService _navigationService;
public HomePageViewModel(INavigationService navigationService)
{
_navigationService = navigationService; //Fails here
}
}
The INavigationService depends on the use of the ViewModelLocator. If you don't use it, then the service cannot be injected. Instead of manually resolving your VM, use the ViewModelLocator.AutowireViewModel attached property. Besides, you don't want to be using your container like that. You only want one composition root when using DI.
Also, I recommend using the latest 6.1 preview. It has a ton of navigation improvements.

Unity Constructor Injection

I am using Prism + Unity in a wpf application using MVVM. I am a beginner with Prism and Unity.
I want to be able to close the current view. The various solutions and articles I've read state that the best way to do this is from the view model. But the view model needs a region manager object in order to close the view. Ok, so let's set up constructor injection. Never tried this before but there are plenty of questions on SO that deal with this.
Let me start with explaining how things are wired together. I have a bootstrapper class that handles the registering of types and instances.
Here is how my view and view model is registered:
container.RegisterType<IViewModel, ViewAccountsViewModel>(new InjectionConstructor(new ResolvedParameter(typeof(RegionManager))));
container.RegisterType<ViewAccountsView>();
Here is the module for the view accounts view:
public class ViewAccountsModule : IModule
{
private readonly IRegionManager regionManager;
private readonly IUnityContainer container;
public ViewAccountsModule(IUnityContainer container, IRegionManager regionManager)
{
this.container = container;
this.regionManager = regionManager;
}
/// <summary>
///
/// </summary>
public void Initialize()
{
regionManager.RegisterViewWithRegion("MainRegion", () => this.container.Resolve<ViewAccountsView>());
}
}
In my ViewAccountsView.xaml, I am setting the data context like so:
<Grid.DataContext>
<vm:ViewAccountsViewModel/>
</Grid.DataContext>
And my view model constructor:
[InjectionConstructor]
public ViewAccountsViewModel(IRegionManager regionManager)
{
if (regionManager == null) throw new ArgumentNullException("regionManager");
this.regionManager = regionManager;
}
When I compile the solution, I receive an error that the type "ViewAccountsViewModel" does not include any accessible constructors. If I add a default constructor to my view model, the view displays but I cannot remove the view from the region. I get an argument null exception.
Here is the code for removing the view:
regionManager.Regions["MainRegion"].Remove(regionManager.Regions["MainRegion"].GetView("ViewAccountsView"));
I am still very much a beginner with IoC and DI. Is there something I have missed?
Unity will handle injecting all dependencies it knows about for you. By default, Unity will call the constructor with the most parameters. You usually use InjectionConstructor to either tell Unity to choose a different constructor when it creates the objects for you, or if you want to pass it custom parameters.
Registration:
container.RegisterType<IViewModel, ViewAccountsViewModel>();
// If you plan to have multiple IViewModels, it will need to have a name
// container.RegisterType<IViewModel, ViewAccountsViewModel>("ViewAccountsViewModelName");
container.RegisterType<ViewAccountsView>();
ViewModel:
// If you decide later you need other dependencies like IUnityContainer, you can just set
// it in your constructor and Unity will give it to you automagically through the power
// of Dependency Injection
// public ViewAccountsViewModel(IRegionManager regionManager, IUnityContainer unityContainer)
public ViewAccountsViewModel(IRegionManager regionManager)
{
this.regionManager = regionManager;
}
View Code Behind:
// If you have a named IViewModel
// public ViewAccountsView([Dependency("ViewAccountsViewModelName")]IViewModel viewModel)
public ViewAccountsView(IViewModel viewModel)
{
this.InitializeComponent();
this.DataContext = viewModel;
}

Categories