Git Product home page Git Product logo

rockmvvmforms's Introduction

RockMvvmForms

RockMvvmForms, a very simple framework for Xamarin.Forms projects.

Principles:

  • Not IoC implemented. Just a basic Service Locator is included RockServiceLocator.
  • Separate completaly the Views from the ViewModels with the static class ViewFactory.
  • Navigation happens in the ViewModel.

Initial setup

Just install the nuget package Install-Package RockMvvmForms in your PCL project.

Create 2 private functions to register services and Views in your Application Forms class as follows:

public partial class App : Application
     {
         private IViewFactory _viewFactory;
 
         public App ()
         {
             InitializeComponent (); 
 
             // Register the services in the Forms Service Locator
             RegisterServices ();
 
             // Register the ViewModels and asociate them to the corresponding Views
             RegisterViews ();
 
             // The root page of your application
             MainPage = new RockNavigationPageService<FirstViewModel>().Create();
         }
 
         private void RegisterServices()
         {
             RockServiceLocator.Current.Register<IMarvelApiService, MarvelApiService> ();
         }
 
         private void RegisterViews()
         {
            ViewFactory.Register<FirstViewModel, FirstView> ();
            ViewFactory.Register<DetailViewModel, DetailView> ();
         }
 
         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
         }
             
     }

ViewFactory

The ViewFactory is the repository for ViewModels and Views.

The ViewFactory is a static class and this is how to register your ViewModels and Views:

private void RegisterViews()
{
    ViewFactory.Register<FirstViewModel, FirstView> ();
    ViewFactory.Register<DetailViewModel, DetailView> ();
}

RockServiceLocator

This is a basic Service Locator. The reason of creating my own Service Locator and not to use DependencyService is because to use DependencyService you need to execute Forms.Init() and this is a bad choice if you want to have your Unit Tests in a nUnit Library Project. This is how to use it:

// Register
private void RegisterServices()
{
    RockServiceLocator.Current.Register<IMarvelApiService, MarvelApiService> ();
}

// Resolve
private readonly IMarvelApiService _marvelService;
public FirstViewModel ()
{
     _marvelService = RockServiceLocator.Current.Get<IMarvelApiService>();
}

ViewModels

The ViewModels have to inherit from ViewModelBase class in order to use them using RockMvvmForms. Once we have done this we access the following capabilities:

  • Navigation - Navigation Service implementation of Xamarin.Forms.

    • PopAsync
    • PopModalAsync
    • PushAsync
    • PushModalAsync
    • DisplayAlert
    • DisplayActionSheet
  • InitAsync - You can override the InitAsync method of the ViewModelBase. This method is executed after the ViewModel constructor and before the View is displayed.

  • View_Appearing - You can override this Event which is happening when the View in appearing.

  • View_Disappering - You can override the disappering event of the View.

Example of how to use the Navigation at the ViewModel:

this.Navigation.PushAsync<DetailViewModel> (new DetailViewModel (character));

Rock Pages Services

To access manually to the ViewFactory, the MainPage initialization it's being done through a Page Services for ContentPages, NavigationPages and MasterDetailPages. These are: RockNavigationPageService, RockContentPageService and RockMasterDetailPageService. My intention is to create new Page Services for Tab pages and Caruossel pages and will be available in future versions.

Enjoy!

rockmvvmforms's People

Contributors

mariolobar avatar ramonesteban78 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

xleon

rockmvvmforms's Issues

Avoid user to use the ViewFactory in their Forms PCL project

We need an Init() function to avoid access to IViewFactory interface in the App.cs
Also the registration of Views and ViewModels should be made maybe through a RockMvvmForms.Register or something like that.

Say Rock if you would like to have this improve :)

RockMvvmForms IoC implementation

We need to prepare a plugin or just add an IoC like Autofac.

This is because the DependencyService of Forms is not testable from a nUnit Library project as it needs to Init Forms to make use of it.

These could cause the following additional implementations:

  • We can no longer use the constructors of the ViewModels to pass parameters. We have to find another way. Maybe in the InitAsync function?.

I will prepare this feature asap.

Concrete instances and Lazy initialization

What about adding a method like this without actually creating the instance right away. It would be on hold until requested with Get<T> and it would improve performance in the case we register lot of things.
It could be something like:

Register<T>(Func<T> createAction) { }
RockServiceLocator.Current.Register<Ball>(() => new Ball());

That would also allow us to use concrete instances:

var ball = new Ball(param1, param2);
RockServiceLocator.Current.Register<Ball>(() => ball);

But that would affect how your locator works, because right now everything you register is interpreted as a Singleton. So, to handle this situation I would implement an API like the following:

Register<T, TImpl>(); // a new instance will be created with every Get<T>();
Register<T>(Func<T> instanceFunc); // instanceFunc will be invoked with every Get<T>();
RegisterSingleton<T, TImpl>(); // same as your current Register<T, Timpl>();
RegisterLazySingleton<T>(Func<T> instanceFunc); // instanceFunc will be invoked just once and the result is saved for later use

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.