异步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