Valerie Linc
Valerie Linc

Reputation: 2831

MVVMCross how to use same events

I have a View with 2 textbox and a button that call an action on a ViewModel to show another View; that what will show in it depends on values of 2 texbox.

For that reason before to call my ViewModel i want to check textbox values and if its are empty show a Dialog. Now to call my ViewModel i have add a binding like this:

 this.AddBindings(new Dictionary<object, string>()
        {

        { btnSearch, "TouchUpInside GoParameterizedCommand" },
        });

as Swiss Binding. Now if i want to use same event to check if my textbox are valorized and don't call GoParameterizedCommand, how could i do?

Upvotes: 1

Views: 341

Answers (1)

Stuart
Stuart

Reputation: 66882

You could bind all your controls to ViewModel properties like:

this.AddBindings(new Dictionary<object, string>()
{
    { btnSearch, "TouchUpInside GoCommand" },
    { text1, "Text MyText" },
    { switch1, "On MyOption" },
   // ...
};

Then inside the GoCommand handler you could put whatever logic you need:

public ICommand GoCommand
{
  get
  {
     return new MvxCommand(() => {
          if (MyOption)
          {
              ShowViewModel<OptionViewModel>();
          }
          else
          {
              ShowViewModel<DetailViewModel>(new { text = MyText });
          }
      });
   }
 }

For showing a dialog - eg an error dialog - then this might be best done using a messenger - sending an error message from the viewmodel. There are a few questions on here about error handling - eg http://slodge.blogspot.co.uk/2012/05/one-pattern-for-error-handling-in.html - plenty of other options are available for giving the user a hint about what to do - eg you could bind the background colour of the text field to an IsMyTextValid property.

Upvotes: 1

Related Questions