public class AViewModel : ViewModelBase
{
#region 构造函数
public AViewModel(object window)
{
this.CurrentView = (A)window;
this.DC_Async = new DelegateCommand(new Action<object>(this.F_Async));
this.DC_Sync = new DelegateCommand(new Action<object>(this.F_Sync));
}
#endregion
#region 属性
private A CurrentView { get; set; }
#endregion
#region 属性-绑定非元素对象
private string p_Param1;
public string P_Param1
{
get { return p_Param1; }
set { p_Param1 = value; this.OnPropertyChanged("P_Param1"); }
}
#endregion
#region 委托命令
public DelegateCommand DC_Async { get; set; }
public DelegateCommand DC_Sync { get; set; }
#endregion
#region 委托方法
/// <summary>
/// 异步
/// </summary>
/// <param name="sender"></param>
private void F_Async(object sender)
{
this.LoadingShow();
DoAsync(() =>
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
this.CurrentView.btnLoadAsync.Content = "异步加载";
}));
this.LoadingHide();
});
}
/// <summary>
/// 同步
/// </summary>
/// <param name="sender"></param>
private void F_Sync(object sender)
{
Task.Run(() =>
{
try
{
this.LoadingShow();
//this.ThrowException("测试异常错误");
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
this.CurrentView.btnLoadSync.Content = "同步加载22222";
}));
Application.Current.Dispatcher.Invoke(new Action(() =>
{
this.CurrentView.btnLoadSync.Content = "同步加载中";
}));
}
catch (Exception ex) { this.BoxErrorMsg(ex.Message); }
finally { this.LoadingHide(); }
});
}
#endregion
#region 私有方法
private async void DoAsync(Action actionComplete)
{
await Task.Run(() =>
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
this.CurrentView.btnLoadAsync.Content = "异步加载中";
}));
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));//程序执行
if (actionComplete != null) { actionComplete.Invoke(); }
});
}
private void F_ffffff(object sender)
{
Task.Run(() =>
{
try
{
this.LoadingShow();
#region 业务
#endregion
}
catch (Exception ex) { this.BoxErrorMsg(ex); }
finally { this.LoadingHide(); }
});
}
#endregion
}
public class ViewModelBase : DependencyObject, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public int PageIndex = 1;
public int PageSize = 20;
public ViewModelBase()
{
IsLoading = Visibility.Collapsed;
}
#region 加载
private Visibility isLoading;
public Visibility IsLoading
{
get { return isLoading; }
set { isLoading = value; this.OnPropertyChanged("IsLoading"); }
}
public void LoadingShow()
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
IsLoading = Visibility.Visible;
}));
}
public void LoadingHide()
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
IsLoading = Visibility.Collapsed;
}));
}
#endregion
#region 抛出异常,提示
public void ThrowException(string msg)
{
throw new Exception(msg);
}
public void BoxSuccessMsg(string msg)
{
MessageBox.Show(msg, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
public void BoxErrorMsg(string msg)
{
MessageBox.Show(msg, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
}
#endregion
}