aspnetde / MvvmNano

The small and smart MVVM framework made with ❤ for Xamarin.Forms.
MIT License
59 stars 10 forks source link
mvvm xamarin xamarin-forms

MvvmNano

The small and smart MVVM framework made with ❤ for Xamarin.Forms.

Build Status NuGet Package
Build status NuGet version
  1. Manifesto
  2. Download
  3. Demo
  4. Getting started
  5. Data Binding
  6. Navigation
  7. Dependency Injection
  8. Messaging
  9. Cleaning up
  10. XAML Support
  11. MasterDetailPage

Manifesto

  1. Each View (aka Page) must have its own View Model.
  2. Views know their View Models, but not vice versa: View Models never know their Views.
  3. Therefore navigation works from View Model to View Model only, without involving the View.
  4. When navigating, passing complex objects along must be possible.
  5. There should be no limits in how to present Views.
  6. View Models must be easily testable, so Dependency Injection is a basic prerequisite.
  7. Both Views and View Models must be easy to clean up.

Download

Install-Package MvvmNano.Forms

Demo

C#: If you are looking for a C# demo, just download this repo and take a look at the demo app which can be found within the /demo folder. Note: It's not using the NuGet packages.

F#: ixmrm01 ported the current C# demo to F# and was kind enough to share it. You can take a look at it here: https://github.com/ixmrm01/MvvmNanoDemo

Getting started

Preliminary remarks

  • MvvmNano ships as three .netstandard 2.0 libraries (MvvmNano.Core, MvvmNano.TinyIoC, and MvvmNano.Forms) and also provides three Portable Class Libraries (PCL) with profile 259 (MvvmNano.Core, MvvmNano.Ninject, and MvvmNano.Forms). NuGet will detect which libraries are needed based on your project type.
  • MvvmNano.Forms references Xamarin.Forms
  • MvvmNano.Ninject references Portable.Ninject
  • MvvmNano.Core does not have and external dependency

Add the NuGet package

You can add MvvmNano easily via NuGet:

Install-Package MvvmNano.Forms

Important: Add it to your Xamarin.Forms library as well as to your native app projects, so NuGet can resolve the right assemblies of the dependencies Xamarin.Forms and Portable.Ninject/.TinyIoC on each target (for example PCL, Xamarin.iOS, Xamarin.Android).

If you want to use our default IoC container, we provide one for .netstandard2.0 and PCL259. The default container for a PCL project is:

Install-Package MvvmNano.Ninject

For .netstandard2.0 projects:

Install-Package MvvmNano.TinyIoC

Add your first View Model and its Page

Your View Model needs to inherit from MvvmNanoViewModel<TNavigationParameter> or MvvmNanoViewModel. Let's start with the latter and thereby without a parameter.

public class LoginViewModel : MvvmNanoViewModel
{
    // ...
}

Now add the Page. Note that by convention it needs to be named after your View Model, except for the ViewModel suffix (so LoginViewModel becomes LoginPage). You also need to inherit from MvvmNanoContentPage<TViewModel>.

public class LoginPage : MvvmNanoContentPage<LoginViewModel>
{
    // ...
}

Set up your App class

Each Xamarin.Forms app has an entry point – a class called App which is derived from Application. Change that base class to MvvmNanoApplication.

Next you are asked to implement the method GetIoCAdapter() which is expected to return an implementation of IMvvmNanoIoCAdapter. Just go with our default choice (MvvmNano.Ninject in a PCL project, which uses Portable.Ninject, MvvmNano.TinyIoC in a .netstandard2.0 project), or go with your own.

You also want to tell your application the first Page and View Model which should be used when the app gets started for the first time. Put this setup inside of OnStart(), but don't forget to call base.OnStart(). This is important in order to set up the Presenter correctly (for more on that see below).

public class App : MvvmNanoApplication
{
    protected override void OnStart()
    {
        base.OnStart();

        SetUpMainPage<LoginViewModel>();
    }

    protected override IMvvmNanoIoCAdapter GetIoCAdapter()
    {
        return new MvvmNanoNinjectAdapter();
    }
}

That's it!

If you now build and run your app(s), you'll see your first Page which is running with it's View Model behind. Nothing spectacular so far, but the fun is just getting started.

Data Binding

Xamarin.Forms comes with really powerful data binding features which you're fully able to leverage with MvvmNano, so we are not reinventing the wheel here.

NotifyPropertyChanged()

MvvmNano View Models implement INotifyPropertyChanged and offer a small helper method called NotifyPropertyChanged() (without the leading I).

private string _username;
public string Username
{
    get { return _username; }
    set
    {
        _username = value;
        NotifyPropertyChanged();
        NotifyPropertyChanged("IsFormValid");
    }
}

As you can see, NotifyPropertyChanged() can be called with and without the name of the property it should be notifying about. If you leave it out, it will automatically use the name of the property you're calling it from.

(Scared from so much boilerplate code? Take a look at Fody PropertyChanged.)

BindToViewModel()

This is a small helper method baked in to MvvmNanoContentPage, which makes binding to your View Model a no-brainer when writing your views (pages) in code:

var nameEntry = new Entry
{
    Placeholder = "Your name"
};

BindToViewModel(nameEntry, Entry.TextProperty, x => x.Username);

Commands

Xamarin.Forms supports ICommand, and so does MvvmNano.

View Model:

public MvvmNanoCommand LogInCommand
{
    get { return new MvvmNanoCommand(LogIn); }
}

private void LogIn()
{
    // ...
}

Page:

BindToViewModel(loginButton, Button.CommandProperty, x => x.LogInCommand);

Commands with parameters

View Model:

public MvvmNanoCommand<string> LogInCommand
{
    get { return new MvvmNanoCommand<string>(LogIn); }
}

private void LogIn(string userName)
{
    // ...
}

Page:

BindToViewModel(loginButton, Button.CommandProperty, x => x.LogInCommand);
BindToViewModel(loginButton, Button.CommandParameterProperty, x => x.Username);