WPF异步命令以及SqlSugar异步增删改查

1、异步

 /// <summary>
 /// 查询全部
 /// </summary>
 /// <returns></returns>
 public async Task<List<IgniteTubeInfo>> QueryListAsync()
 {
     return await db.Queryable<IgniteTubeInfo>().ToListAsync();
 }
 /// <summary>
 /// 跟据Id查询
 /// </summary>
 /// <returns></returns>
 public async Task<List<IgniteTubeInfo>> QueryByIdAsync(int id)
 {
     return await db.Queryable<IgniteTubeInfo>().Where(it=>it.Id==id).ToListAsync();
 }

 /// <summary>
 /// 跟据Name查询
 /// </summary>
 /// <returns></returns>
 public async Task<List<IgniteTubeInfo>> QueryByNameAsync(string name)
 {
     return await db.Queryable<IgniteTubeInfo>().Where(it => it.IgniteName==name).ToListAsync();
 }

 /// <summary>
 /// 修改
 /// </summary>
 /// <param name="igniteTubeInfo"></param>
 /// <returns></returns>
 public async Task UpdateAsync(IgniteTubeInfo igniteTubeInfo)
 {
      await db.Updateable(igniteTubeInfo).ExecuteCommandAsync();
 }

 /// <summary>
 /// 跟据Id删除
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
  public async Task DeleteAsync(int id)
  {
     await db.Deleteable<IgniteTubeInfo>().Where(u => u.Id == id).ExecuteCommandAsync();
 }

2、命令

//自定义一个异步命令
 public class AsyncCommand : ICommand
 {
     private bool _isExecuting;
     private readonly Func<Task> _execute;
     private readonly Func<bool> _canExecute;

     public AsyncCommand(Func<Task> execute, Func<bool> canExecute = null)
     {
         _execute = execute;
         _canExecute = canExecute;
     }

     public bool CanExecute(object parameter)
     {
         return !_isExecuting && (_canExecute?.Invoke() ?? true);
     }

     public async void Execute(object parameter)
     {
         if (CanExecute(parameter))
         {
             try
             {
                 _isExecuting = true;
                 await _execute();
             }
             finally
             {
                 _isExecuting = false;
             }
         }
     }

     public event EventHandler CanExecuteChanged
     {
         add { CommandManager.RequerySuggested += value; }
         remove { CommandManager.RequerySuggested -= value; }
     }
 }
 //使用
  private AsyncCommand _canelCommand;
 public AsyncCommand CancelCommand  =>
     _canelCommand ?? (_canelCommand = new AsyncCommand(ExecuteCanel));

 private void ExecuteCanel()
 {
 }
posted @ 2023-09-21 20:10  孤沉  阅读(168)  评论(0编辑  收藏  举报