When not to use View Services in MVVM design pattern?

I’m using the MVVM design pattern in my application which is comprised of,

  • A Xamarin.IOs project (View Layer)
  • A Net Standard project (Common Layer)
  • A Xamarin.Android project (in the future) (View Layer)

In the Xamarin.IOs project, there’s a Delegate class (NotificationCenterDelegate extending UNUserNotificationCenterDelegate) which triggers a method (WillPresentNotification) on receiving a notification. This method should inform a ViewModel (BasePageViewModel) that the method was invoked. The BasePageViewModel is not injected to the Delegate class.

There’s a couple of approaches I can use here,

  1. Inject a ViewModel (ControlViewModel). Then invoke a method in the ViewModel from the WillPresentNotification method.

    public void WillPresentNotification(/* parameters */) {     ControlViewModel.UpdateCount(); } 

    In ControlViewModel I invoke an event that is captured and handled in BasePageViewModel

  2. Implement an Interface (IDelegateViewService) in the Delegate class that has an Event Handler. Trigger the event in the WillPresentNotification method.

    public class NotificationCenterDelegate : UNUserNotificationCenterDelegate, INotificationCenterDelegate {     public void WillPresentNotification(/* parameters */)     {         CountUpdated?.Invoke(this, null);     } }  public interface IDelegateViewService {     event EventHandler CountUpdated; } 

    Then capture handle the event in the BasePageViewModel.

I prefer the first approach as it doesn’t use an interface to invoke something in the ViewModel. Because as I feel using ViewServices reduces the ability of sharing code among different platforms. And I feel ViewServices should be only used if it’s the only approach available.

Can anyone comment on these two design approaches? I would like to get some theory involving why one is better than the other or why a completely different design is much better!

MVVM viewmodel to view correlation in adaptive UI

I am making an mvvm application for the uwp platform and I am looking for advice on the following matter.

Take for example the windows 10 e-mail application, which when full screen displays both the e-mail list and e-mail content in one page, while in smaller form factors the content is displayed in separate pages.

What is the best way to correlate views with viewmodels in this scenario?

My thoughts are

  1. A big view model which will be the data context for all the views in this scenario.

  2. Two view models, one for the e-mail list and one for the e-mail content, set as the data context for different parts of the page or different pages.

  3. Three view models. The two view models from the previous point and another one to coordinate them.

What do you think is best? My problem mostly stems from the fact that in one case you actually navigate to another page, and have to put logic in navigation events that don’t exist in the other case.

Mvvm pattern android – Java

I’m starting MVVM pattern and would like to know if I’m doing well with this. Here is the link from my repository on GitHub: https://github.com/Hbeto021/mvvm-project.

Does my application even implement MVVM? Are there any suggestions or opinions about my code? How am I going?

Bellow a quick example of how I’m doing. Please see my application in my repository, it’s just a simple project.

binding.getViewModel().getAllUsers().observe(this, new Observer<List<User>>() {     @Override     public void onChanged(@Nullable List<User> pessoas) {         //update recyclerview when my list my database that returns liveData is changed         userAdapter.addUserToList(pessoas);     } }); 

Addition Program using wpf through MVVM

I’m new to MVVM and WPF. Please suggest whether this is okay or I need to correct my understanding of MVVM which I confess is very limited as at the moment.

I have created a simple addition application which gets two number as input and provide the added number after clicking the Button.

Please give me your honest (and brutal) opinion, since that will help me improve the most.


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel;  namespace addition.Model {     class Number     {   public int number1             { get; set; }     public int number2         { get; set; }    public int number3         { get; set; }       } } 


using addition.Model;  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input;   namespace addition.ViewModel {     class ViewModel : INotifyPropertyChanged     {         private Number n1 = new Number();         int num, num1;           public RelayCommand AddNew { get; set; }         private string _number1;  public string FirstArgument {      get { return this._number1; }     set     {         this._number1 = value;         if (int.TryParse(_number1.ToString(), out num))         {             this.n1.number1 = num;             this.OnPropertyChanged("FirstArgument");          }         else { MessageBox.Show("The given Value is not a Number "); }      } } private string _number2;          public string secondargument         {             get { return this._number2; }              set             {                 this._number2 = value;                 if (int.TryParse(_number2.ToString(), out num1))                 {                     this.n1.number2 = num1;                     this.OnPropertyChanged("secondargument");                 }                 else { MessageBox.Show("The given Value is not a Number "); }              }         }          private string _number3;          public string Addedargument         {             get { return this._number3; }             set             {                 this._number3 = value;                 this.OnPropertyChanged("Addedargument");             }         }          public  ViewModel()     {          AddNew = new RelayCommand(o => AddNumbers());      }       private void AddNumbers()     {         var a = this.FirstArgument;         var b = this.secondargument ;     var c = (Convert.ToInt32(a) + Convert.ToInt32(b)).ToString();     MessageBox.Show(c);     Addedargument = c;       }      #region INotifyPropertyChanged Members      public event PropertyChangedEventHandler PropertyChanged;     private void OnPropertyChanged(string propertyName)     {         if (PropertyChanged != null)         {             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));         }     }     #endregion      } } 


<Window x:Class="addition.Window1"          xmlns:vm="clr-namespace:addition.ViewModel"      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window1" Height="300" Width="300">     <Window.DataContext>         <vm:ViewModel />     </Window.DataContext>     <Grid>          <Label Height="28" Margin="28,54,0,0" Name="Number1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="48">Number</Label>         <TextBox Height="28" Margin="112,56,46,0"  Text ="{Binding Path = FirstArgument}"   Name="textBox1" VerticalAlignment="Top" />         <Label Margin="28,106,0,128" Name="Number2" Width="58" HorizontalAlignment="Left">Number1</Label>         <TextBox Height="28" Margin="112,120,46,120" Text ="{Binding  Path = secondargument}" Name="textBox2" />         <Label Height="28" Margin="28,0,0,75" Name="label1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="58">Number2</Label>         <TextBox Height="23" Margin="112,0,46,68" Name="textBox3" Text="{Binding Path = Addedargument}" VerticalAlignment="Bottom" />         <Button Height="23"  HorizontalAlignment="Left" Margin="39,0,0,16" Name="button1" VerticalAlignment="Bottom" Width="75" Command="{Binding AddNew}">Button</Button>     </Grid> </Window> 

When to use The Messenger (Mediator) Pattern in MVVM design

I got confused with the role of the Messenger in MVVM. I see contradictory articles about it.

This Article by MSDN by:

Communicating from the View-Model to the View
Observant readers will notice that Figure 1 has one arrow missing: in this figure, there is no way for the View-Model to communicate with the view. As mentioned earlier, the View-Model should ideally have no knowledge of the view that it is attached to. In fact, it is very common for a given View-Model to be attached to multiple views—for instance, because one view might become too complex and be split into two pages. To guarantee flexibility, the View-Model must have only an abstracted knowledge of what the view can do.
There are multiple ways to solve this issue. The two solutions that I propose here are using MVVM Light’s Messenger class and using view services.

generally saying that the messages should be from the View-Model to the View in case needed.

Also in this article, it warns about using the Messenger widely as it causes code to be less readable (which I agree with).

But on Another Article
It’s used differently to communicate messages between View-Models

UML Diagram showing Messenger communication between View-Models

In this scenario, I don’t get the “Why VM cant have reference to another VM?”
as the referenced View-Model can easily be mocked and tested.