Get your Xamarin.Forms Pages and ViewModels into the real DI world.
- Create your container at your Platform
- Register your platform Services
- Hand the IoC container to your App
- Register your shared Services
- Register your Pages
- Initialize the Navigation
- Navigate like a Pro 😎
Note: All files and Lines here base on the Sample project
1 new Ioc container MainActivity.cs
var ioc = new FinalIoc();
2 Register platrform services MainActivity.cs
ioc.RegisterService<IPlatformDependentService, AndroidPlatformService>();
3 LoadApplication MainActivity.cs
LoadApplication(new App(ioc));
Note: "App.xaml.cs" needs the FinalIoc as constructor parameter
4 Register common code services App.xaml.cs
container.RegisterService<IDemoService, DefaultDemoService>();
5 Register pages App.xaml.cs
container.RegisterPage<LoginPage, LoginPageViewModel>();
container.RegisterPage<UserPage, UserPageViewModel>();
6 Build the Navigator App.xaml.cs
new FinalNavigator(this, container).Build<LoginPage>();
7 Navigate LoginPageViewModel.cs
To be able to Navigate include the "INavigationService" interface in the Page/ViewModel constructor:
public LoginPageViewModel(INavigationService navigationService)
Use the NavigationService to navigate further with all your services always on hand
await FinalNavigator.Instance.PushAsync<UserPage>(new NavigationParameter
{
Type = EParameterType.ViewModel,
Parameter = Username,
});
Note you also be able to use the following class constructors:
public PageParameter(object parameter)
public ViewModelParameter(object parameter)
All methods get invoked on App.MainPage.Navigation
- PushAsync(params NavigationParameter[] userParameters)
- PopAsync()
- PopToRootAsync()
- PushModalAsync()
- PopModalAsync()