Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WP - Async Command MVVM show message 'Loading'

$
0
0

I have this code in the VM:

 public class MensajesTablonViewModel : NotificationenabledObject
    {
         ...
         public ICommand getMensajesTablon
        {
            get;
            private set;
        }
         public MensajesTablonViewModel()
        {
            mensajes = new ObservableCollection<MensajeTablon>();
            servicioMensajes.getMensajesTablonCompletado += (s, a) =>
            {
                //mensajes = new ObservableCollection<MensajeTablon>(a.mensajes);
                insertarNuevosMensajes(a.mensajes);
                this.OnPropertyChanged("Mensajes");
            };
            this.getMensajesTablon = new ActionCommand<string>(this.onGetMensajesTablon);
            this.postMensajesTablon = new ActionCommand<string>(this.onPostMensajesTablon);
            servicioMensajes.getMensajesTablon();

        }

        private void onGetMensajesTablon(string useless)
        {
            servicioMensajes.getMensajesTablonFromServer("1");
            //servicioMensajes.getMensajesTablon();
        }
        ...

My async method:

public async void getMensajesTablonFromServer( string idFacultad) { dbConn = new SQLiteConnection(DB_PATH); var idMensaje = dbConn.Query<MensajeTablon>("select MAX(identificador) as identificador from MensajeTablon where identificadorTablon = "+ idFacultad + ";"); List<MensajeTablon> mensajesNuevos = new List<MensajeTablon>(); var idemax = idMensaje[0].identificador; string response = await Comunicacion.getMensajes(AplicationSettings.getToken(),idemax+"",idFacultad);

//Do something

..... if (getMensajesTablonCompletado != null) { getMensajesTablonCompletado(this, new MensajesTablonEventArgs(mensajesNuevos)); } }


This is my DelegateCommand:

public class ActionCommand<T> :ICommand
    {
        //Accion que se realiza
        Action<T> action;
        //Constructor
        public ActionCommand(Action<T> accion)
        {
            action = accion;
        }
        //Método que dice si se puede ejecutar o no el comando.
        public bool CanExecute(object parameter)
        {
            return true;
        }
        //El evento que se lanza.
        public event EventHandler CanExecuteChanged;
        //Ejecuta el evento.
        public void Execute(object parameter)
        {
            action((T)parameter);
        }
    }

And this is my View:

<Button x:Name="CargarMensajes"
                            Content="Cargar Comentarios" Grid.Row="1"
                            Command="{Binding getMensajesTablon}"
                            CommandParameter="" Click="CargarMensajes_Click" />


How can I display a loading message (in the view) while the async task is getting called?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>