How to implement Async Command

前端 未结 4 1780
轻奢々
轻奢々 2021-02-19 16:02

Although I have understood async programming with c# somehow, still don\'t get why async with void is not the better solution, then when I want to improve my Xamarin Forms code

4条回答
  •  清歌不尽
    2021-02-19 17:00

    To anyone interested: Brandons solution above does not requery the CanExecute automatically and requires RaiseCanExecuteChanged(). To change this, you can exchange

        public event EventHandler CanExecuteChanged
        {
            add => _weakEventManager.AddEventHandler(value);
            remove => _weakEventManager.RemoveEventHandler(value);
        }
    

    with

        public event EventHandler CanExecuteChanged {
            add => CommandManager.RequerySuggested += value;
            remove => CommandManager.RequerySuggested -= value;
        }
    

    and remove

    public void RaiseCanExecuteChanged() => _weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(CanExecuteChanged));
    

    This fixed the problem for me.

提交回复
热议问题