Block Form Button - Xamarin

Asked

Viewed 261 times

2

I would like to know how to block an action button in Xamarin.Forms if certain variables are not filled in. Follow the code I have in my Viewmodel: //This is the builder of my Viewmodel

public MinhaViewModel()
    {            
        //os campos dessa variável precisam estar preenchidos
        this.cidadeDestino = new CidadeDestino();

        //Botão para enviar a busca que precisa estar bloqueado.
        this.BuscaPartidasCommand = new Command(() =>
        {

          MessagingCenter.Send(cidadeDestino, "BuscaCidade");

        });       
    }

The destination city variable is the type of a class I created, it has two string type parameters, that’s it, and these two parameters must be different from "null" so that the button can perform its action. I thank anyone who can help.

1 answer

2


In the pattern MVVM in the Xamarin.Forms usually this is dealt with by the Command called CanExecute.

With it, when associating a command to a button, every time there is change in the CanExecute and the response is False, the button to which the command is associated is also disabled.

Developing on the scenario you have, we would have something like what I’m going to show you next.

Your model would be something like this:

public class CidadeDestino : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string nome;
    public string Nome
    {
        get { return nome; }
        set
        {
            if (value != nome)
            {
                nome = value;
                OnPropertyChanged("Nome");
            }
        }
    }

    private string siglaEstado;
    public string SiglaEstado
    {
        get { return siglaEstado; }
        set
        {
            if (value != siglaEstado)
            {
                siglaEstado = value;
                OnPropertyChanged("SiglaEstado");
            }
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        if (!string.IsNullOrWhiteSpace(propertyName))
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

Considering this scenario, in short, its condition of the CanExecute is "City name and state acronym must be different from null and void".

So, continuing the development...

On screen (XAML)

...
<Entry Text="{Binding NomeCidadeDestino}"
       Placeholder="Nome da Cidade"
       .../>
<Entry Text="{Binding SiglaEstadoCidadeDestino}"
       Placeholder="UF"
       .../>
<Button Text="Confirmar"
        Command="{Binding ComandoConfirmar}"
        .../>
...

* - Although I defaced the screen in XAML, if the definition is done via C# the effect is the same.

And building the other layer...

Na View Model

public class MinhaViewModel
{
    private CidadeDestino cidadeDestino = new CidadeDestino();
    public Command ComandoConfirmar { get; }

    public string NomeCidadeDestino
    {
        get { return this.cidadeDestino?.Nome; }
        set
        {
            cidadeDestino.Nome = value;
            ComandoConfirmar.ChangeCanExecute(); // Aqui você 'informa' que o estado do comando deve ser reavaliado
        }
    }

    public string SiglaEstadoCidadeDestino
    {
        get { return this.cidadeDestino?.SiglaEstado; }
        set
        {
            cidadeDestino.SiglaEstado = value;
            ComandoConfirmar.ChangeCanExecute();
        }
    }

    public MinhaViewModel()
    {
        ComandoConfirmar = new Command(ExecutarConfirmar, () => !string.IsNullOrWhiteSpace(cidadeDestino?.Nome) && !string.IsNullOrWhiteSpace(cidadeDestino?.SiglaEstado));
    }

    private void ExecutarConfirmar()
    {
        MessagingCenter.Send(cidadeDestino, "BuscaCidade");
    }
}

And the result of that implementation would be:

Exemplo da implementação funcionando

Now just take this idea and apply in your real scenario that should solve. That same CanExecute It also uses a common property in Viewmodels, which generally alternates the visibility of those "loading" marbles. The same idea applies.

Let me know if there’s any doubt left.

I hope this helps.

  • 1

    Hello Diego, your answer helped me too much, I had to make small changes because of my code, but the idea was the same. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.