The use of Pushasync<Tviewmodel> in MVVM allows calling multiple pages. How to avoid this?

Asked

Viewed 31 times

0

I have a Button that triggers a Command whose Action is a Pushasync< Tviewmodel >(). The problem is that if you click the button more than once before opening the new screen, it will open more than one screen. And this happens throughout the MVVM when using Pushasync. I have tried to make use of Baseviewmodel’s Isbusy, to no avail. I don’t know what else to do and I am seriously considering leaving MVVM because of it. If anyone can help me I’d appreciate it.

 public async void ActionAdicionar() {    
            if (IsBusy) return;
            else IsBusy = true;

            await PushAsync<AdicionarItemViewModel>();

            IsBusy = false;
        }
  • 1

    tried to put a await in the actionAdd call on the button Event Handler?

2 answers

0

Use a counter, which if there is more than 1 Voce will not perform the Pushasync function. And when you go back/start the original screen, reset the counter.

public async void ActionAdicionar() {  
    int contador = 0;
    if (contador == 0) 
    {
       await PushAsync<AdicionarItemViewModel>();
       contador++;
    }    
 } 

0

The event runs fast and using counters for this case is not enough. With use of Canexecute the system will block

Command:

Command command = new Command(x => { ActionAdicionar(); }, (x) => { return podeExecutar; });

His method:

public async void ActionAdicionar()
{
    podeExecutar = false;
    
    await PushAsync<AdicionarItemViewModel>();
    
    podeExecutar = true;
}

Auxiliary variable:

bool podeExecutar = true;

Browser other questions tagged

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