Search code examples
c#xamarinmvvmcross

Capture Alert Dialog Button Event


I have added Acr.UserDialogs plugin and called as follows, I could able to see a simple alert with a OK button. So far so good, but I wonder how to capture OK button click event?

public void Save()
{
  if (!isExit)
  {
     OnExit(this, null);
  }
  else
  {
     IsValid = false;
     Mvx.Resolve<IUserDialogs>().Alert("it is not valid");
  }
}

Solution

  • Just pass a AlertConfig and set the OnOk action.

    var alertConfig = new AlertConfig {
        Message = "it is not valid",
        OkText = "Okely",
        OnOk = () => { Debug.WriteLine("ok pressed"); }
    };
    
    Mvx.Resolve<IUserDialogs>().Alert(alertConfig);
    

    UPDATE for Version 7.0+:

    OnOk was renamed to OnAction see: https://github.com/aritchie/userdialogs/blob/master/src/Acr.UserDialogs/AlertConfig.cs#L16

    var alertConfig = new AlertConfig {
        Message = "it is not valid",
        OkText = "Okely",
        OnAction = () => { Debug.WriteLine("ok pressed"); }
    };
    
    Mvx.Resolve<IUserDialogs>().Alert(alertConfig);