Rajan
Rajan

Reputation: 131

MVVM framework for Xamarin.Forms

Can someone explain What benefits an MVVM framework such as ReactiveUI or MVVM Light provides to Xamarin.Forms application? we can implement INotifyPropertyChanged in our viewmodels without using any of these frameworks. Am I missing something?

Upvotes: 4

Views: 1037

Answers (5)

Stephen Marcus
Stephen Marcus

Reputation: 307

The plain truth is that MVVM Frameworks (and their IOC Containers) are short-cuts to save you time. They come at an extraordinary expense:

They cause a simplistic alignment between views, view models and models. Only one can really belong to another. This is not "IOC" -- it's a hard-coded assignment based on a file naming convention such as this:

MainPage

MainViewModel

MainModel

In a real injection scenario, any number of models could serve any number of view models, provided they implement the proper interface. Also, any number of view models can serve any number of views as long as those view models support the views' interface(s).

MVVM Frameworks create classes through a reflection trick using a string prefix. In the example earlier, the string "Main" allows us to find the page, the view model and the model. But this is not view-model-to-view-model navigation. It is string to view model navigation.

The frameworks create classes using Activator.CreateInstance, which creates havoc on the compile-time linker.

The frameworks instantiate using hyper-simplistic constructor parameter logic. Modern programs use instantiation as a form of control. In these frameworks, there is no real control.

The complete code for these remarks is at https://github.com/marcusts/xamarin-forms-annoyances. See the solution called MvvmAntipattern.sln.

The GitHub site also provides links to a more detailed discussion on this topic.

Upvotes: 1

Delari Jesus
Delari Jesus

Reputation: 411

I have been using with great success with Askaiser.Mobile.Pillar, and I do not see anything wrong, I am still investigating, but this has become a great candidate for my projects. Doc

I hope I have helped.

A greeting

Upvotes: 1

Nurhak Kaya
Nurhak Kaya

Reputation: 1781

The Model-View-ViewModel (MVVM) architectural pattern was invented with XAML in mind. The pattern enforces a separation of the XAML user interface (the View) from the underlying data (the Model) through a class that serves as an intermediary between the View and the Model (the ViewModel). The View and the ViewModel are often connected through data bindings defined in the XAML file. The BindingContext for the View is usually an instance of the ViewModel. Please click here to see more details and sample code.

You don't need to use ReactiveUI or MVVM Light, you can just use INotifyPropertyChanged interface to notify clients that a property value has changed.Below, you can see my ViewModelBase class code, hope this helps you out;

public class ViewModelBase : INotifyPropertyChanged
{
    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

In my ViewModel here is how I inherit from ViewModelBase and how I create my property;

public class ImageButtonViewModel : ViewModelBase
{
 // ...
 private string header;
 public string Header
 {
      get
      {
          return header;
       }
      set
        {
          header = value;
          OnPropertyChanged();
         }
  }
// ...
}

Upvotes: 1

Daniel Luberda
Daniel Luberda

Reputation: 7454

I'm an author of a small MVVM framework for Xamarin.Forms (https://github.com/daniel-luberda/DLToolkit.PageFactory). I'll try to point some advantages of using it as some of features are common for other frameworks too:

  • Ready to use INotifyPropertyChanged implementation (just inherit from BaseViewModel or BaseModel)
  • ViewModel oriented Navigation (you can move your navigation logic to view models)
  • Built-in messaging to easily pass data/arguments between pages or view models
  • Page caching and reusing (better UI experience as views/pages are reused)
  • Every page has access to typed ViewModel instance which is automatically instantiated and wired to BindingContext

Those features vary between libraries. Some of more advanced MVVM frameworks offer you a possibility to move your entire project to a different platforms/sdks as they have multi platform support. It may be a nice deal for a larger projects.

Sample code to illustrate some features (the code is called from a ViewModel):

PageFactory.GetPageFromCache<SecondPageModel>()
    .ResetPageModel()
    .SendActionToPageModel((model) => { model.Message = "Hello World!"; })
    .PushPage();

Upvotes: 2

JKennedy
JKennedy

Reputation: 18799

There is more to an MVVM Framework than just INotifyPropertyChanged. To give just a couple of examples there are:

  • RelayCommand - A class that can provides an implementation of the ICommand interface and allows you to bind a delegate to the view. Useful for buttons
  • EventToCommand - This allows you to bind an event to a command in your view model. Instead of having to use code behind for UIElement Events

These are just two classes that are provided by an MVVM Framework such as MVVMLight

By using MVVMLight it means that you do not have to implement INotifyPropertyChanged in every project you just make sure your ViewModel inherits from ViewModelBase and you also don't have to write your own implementation in every project for the two classes mentioned above.

The final thing to mention is that you can also install code snippets for MVVMLight. Meaning that writing code is EVEN faster!

For example if you would like a property that raises property changed as well just use the mvvmlight property snippet. And similarly if you would like a RelayCommand property you can just use the mvvmlight RelayCommand snippet

Upvotes: 1

Related Questions