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()
 {
 }

本文作者:孤沉

本文链接:https://www.cnblogs.com/guchen33/p/17720845.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   孤沉  阅读(177)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开