MVVM Best Practices

Hi guys,

I'm trying to build my app using MVVM. When it comes to displaying data, everything goes well, I have a Page with its View Model and a number of Views which share the same ViewModel where all the logic is done in the ViewModel.

However when I'm not sure what is the best approach when you have a form. I have a page, with a view which contains a number of entries and a button, the button will send the data to the view model and via the Command parameter. What if the Command Method (within the ViewModel) wants to return something back to the View (such as incorrect parameters) and the view will have to display an alert message for example? How will the ViewModel communicate with the View?

Thanks
Shaun

Tagged:

Best Answer

Answers

  • JMarcusJMarcus USMember ✭✭✭

    The view model communicates with the view by implementing INotifyPropertyChanged. Everything about the view (validation status, field values, etc) should be held by the view model. So when a command fires, if a field doesn't pass validation you could have a UsernameIsValid field which becomes false. There are several ways you could go about it but essentially when the view model changes the view will automatically reflect the change if you have implemented INotifyPropertyChanged correctly and bound up all the properties on the view.

  • ShaunGrechShaunGrech USMember ✭✭

    Hi Jeremy,

    Thank you for your answer. Lets say I have a simple login form. The Button will make a Command Call to the ViewModel which will return either true or false. If its true I want to display an alert in the view saying that login was successful. What should the View be listening to, so that it knows when to display the Alert box?

    Thanks again
    Shaun

  • JMarcusJMarcus USMember ✭✭✭

    You could use a plugin (Acr.UserDialogs) or call App.Current.MainPage.DisplayAlert() from within the ViewModel to display the alert. The View itself wouldn't make the call.

  • ShaunGrechShaunGrech USMember ✭✭

    Is there a way how I can use triggers from the code? I can't seem to find any examples. Basically I have some behaviors for my entries, and I want to trigger the Login Button's "IsVisible" property based on the 'IsValid' property from my entry's behavior:

        public class PEntry : Entry
            {
                public PEntry ()
                    : base ()
                {
                }
    
                /// <summary>
                /// Determines whether this instance is valid.
                /// </summary>
                /// <returns><c>true</c> if this instance is valid; otherwise, <c>false</c>.</returns>
                public bool IsValid 
                {
                    get{
                        // iterate through each entry's validator behaviors
                        foreach (var behavior in this.Behaviors) {
                            var validatorBehavior = behavior as IValidatorBehavior;
                            if (validatorBehavior != null) {
                                if (!validatorBehavior.IsValid) {
                                    return false;
                                }
                            }
                        }
    
                        // default to true
                        return true;
                    }
                }
            }
    
  • ShaunGrechShaunGrech USMember ✭✭

    Hi guys, anyone can help me out with this? I just need to find an example where I can use triggers from the code (not in XAML). This way if my textfield.IsValid status is false, I call the trigger.

    Thanks again
    Shaun

Sign In or Register to comment.