【WPF】Async MVVM Demo源码研究

763d5fc2a0e22629dcae27576f13f2f8.png

异步MVVM demo源码 思维导图

异步MVVM源码思维导图 浏览

主窗体代码:

using AsyncDemo.Data;
using AsyncDemo.EvtArgs;
using AsyncDemo.Properties;
using AsyncDemo.Services;
using AsyncDemo.Veiw;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;


namespace AsyncDemo.ViewModel
{
    class MainWindowViewModel : ViewModelBase
    {
        AsyncDemoHelper mAsyncDemoHelper;
        //自定义命令
        private RelayCommand mStartDemoCommand; 
        private RelayCommand mClearListCommand;
        IDialogService mProgressDialogService;//进度条对话框


        public string StartButtonText { get; } = Resources.MainWindowViewModel_StartButtonText; //开始按钮文本
        public string ClearButtonText { get; } = Resources.MainWindowViewModel_ClearButtonText;//清空按钮文本
        public string ResultTextBlockText { get; } = Resources.MainWindowViewModel_TextBlockText; //主窗口 文本框文本


        /// <summary>
        /// Sets and gets the listBox property
        /// 设置和获取 listBox 属性
        /// </summary>
        public ObservableCollection<string> ListBox { get; set; }  //列表框绑定的 string集合


        public MainWindowViewModel(IDialogService dialogService)
        {
            base.ViewTitle = Resources.MainWindowViewModel_Title; //获取主窗体视图标题
            
            // Passing an IDialog service allows us to mock up this service
            // and unit test this viewmodel with the mock.
            mProgressDialogService = dialogService; //进度条对话框
            
            ListBox = new ObservableCollection<string>();  //初始化列表框绑定的数据
            mAsyncDemoHelper = new AsyncDemoHelper(); //初始化子任务辅助类
            mAsyncDemoHelper.WorkPerformed += new EventHandler<WorkPerformedEventArgs>(OnMessageReceived); //子任务处理完毕事件绑定处理函数  委托
        }


        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// start the demo.返回在调用时尝试启动演示的命令。
        /// </summary>
        public ICommand StartDemoCommand
        {
            get
            {
                if (mStartDemoCommand == null)
                {
                    mStartDemoCommand = new RelayCommand(param => OnStartButtonClick());
                }


                return mStartDemoCommand;


            }
        }


        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to clear the list box.返回调用时尝试清除列表框的命令。
        /// </summary>
        public ICommand ClearListCommand
        {
            get
            {
                if (mClearListCommand == null)
                {
                    mClearListCommand = new RelayCommand(param => OnClearListClick());
                }


                return mClearListCommand;


            }
        }


        /// <summary>
        /// Fired when a command is received from the start button
        /// 当从开始按钮接收到命令时触发
        /// </summary>
        private async void OnStartButtonClick()
        {
            ShowProgressDialog();
            ListBox.Add("Starting demo!");


            string result = await mAsyncDemoHelper.DoStuffAsync();
            ListBox.Add(result);


            Application.Current.Dispatcher.Invoke(() =>
            {
                mProgressDialogService.CloseDialog();
            });


        }


        /// <summary>
        /// Async method to run the progress dialog on a background thread while the UI
        /// Stays responsive  在 UI 保持响应时在后台线程上运行进度对话框的异步方法
        /// </summary>
        private async void ShowProgressDialog()
        {
            await Task.Run(() =>
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    mProgressDialogService.ShowDialog();
                });
            });
        }


        /// <summary>
        /// Clear the list box in the main window清除主窗口中的列表框
        /// </summary>
        private void OnClearListClick()
        {
            ListBox.Clear();
        }


        /// <summary>
        /// Callback fired each time some work is performed
        /// 每次执行某些工作时都会触发回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnMessageReceived(object sender, WorkPerformedEventArgs args)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                ListBox.Add(args.Data);
            });
        }
    }
}

The End

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值