VisualStudio/WPF

[WPF] Async RelayCommand 사용 방법

usingsystem 2023. 1. 5. 14:00
728x90
  • Nuget에서 Microsoft.Toolkit.Mvvm 다운로드

 public class MainViewModel : INotifyPropertyChanged
    {
        private int progressValue;
        public ICommand TestClick { get; set; }
        public MainViewModel()
        {
            TestClick = new AsyncRelayCommand(ExcuteMyButton);
        }

        public async Task ExcuteMyButton()
        {
            int w = 0;
            Task<int> task1 = Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    w = i;
                    Thread.Sleep(500);
                    ProgressValue += 10;
                }
                return 5;
            });
            w = await task1;
            MessageBox.Show(w + "z");
        }
}
728x90