using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DevExpress.Utils.MVVM; using DevExpress.Mvvm.POCO; using DevExpress.Mvvm; using DevExpress.XtraSplashScreen; using DevExpress.XtraBars.Alerter; using DevExpress.XtraBars.ToastNotifications; using System.Windows.Forms; using DevExpress.XtraWaitForm; using System.Threading; using DevExpress.XtraEditors; using DevExpress.Utils.MVVM.Services; /// /// ViewModel基类 /// GetService方法不是线程安全的,避免后台线程调用,可使用BeginInvoke切换到UI线程 /// public abstract class BaseVm { /// /// 切换到UI线程直接委托 /// /// UI线程要执行的委托 public void Invoke(Action action) { try { IDispatchService.Invoke(action); } catch (Exception) { //退出时这里可能会出错,此时可忽略 } } /// /// 切换到UI线程直接委托 /// /// UI线程要执行的委托 public async void BeginInvoke(Action action) { await IDispatchService.BeginInvoke(action); } /// /// 向其它ViewModel发送数据 /// /// 数据泛型类型,需要和Register中的类型一致 /// 数据 /// 令牌,需要和Register中的令牌一致 public void Send(T data, string token) { DevExpress.Mvvm.Messenger.Default.Send(data, token); } /// /// 注册接收其它ViewModel数据的回调 /// 需要注意callback执行线程可能不是UI线程,callback操作绑定对象需要调用Invoke或BeginInvoke /// /// 数据泛型类型,需要和Send中的类型一致 /// 数据 /// 令牌,需要和Send中的令牌一致 public void Register(Action callback, string token) { DevExpress.Mvvm.Messenger.Default.Register(this, token, callback); } protected DialogResult DialogResult; public void UnRegister() { DevExpress.Mvvm.Messenger.Default.Unregister(this); } /// /// 选择目录(取消则返回null) /// /// public string DialogChooseDir() { if (!IFolderService.ShowDialog()) return null; return IFolderService.ResultPath; } /// /// 选择文件(取消则返回null) /// /// such as "文本文件|*.txt" /// 是否支持多选 /// public string DialogChooseFile(string filter = "文本文件|*.txt", bool multiselect = false) { IOpenFileService.Filter = filter; IOpenFileService.Multiselect = multiselect; if (!IOpenFileService.ShowDialog()) return null; return IOpenFileService.GetFullFileName(); } /// /// 保存文件(取消则返回null) /// /// /// public string DialogSaveFile(string fileName) { if (!ISaveFileService.ShowDialog(null, "", fileName)) return null; return ISaveFileService.GetFullFileName(); } /// /// 从右下角弹出通知 /// /// public void ShowNotify(string msg) { Invoke(async () => { var notify = INotifyService.CreatePredefinedNotification("消息", "\r\n" + " " + msg, null); await notify.ShowAsync(); }); } /// /// 从右下角弹出通知 /// /// public void ShowNotifyError(string msg) { Invoke(async () => { var notify = INotifyService.CreatePredefinedNotification("错误", "\r\n" + " " + msg, null); await notify.ShowAsync(); }); } /// /// 显示提示消息 /// /// public void ShowMsg(string msg) { Invoke(() => { IMsgBoxService.ShowMessage(msg, "提示", MessageButton.OK, MessageIcon.Information); }); } /// /// 显示警告消息 /// /// public void ShowWarning(string msg) { Invoke(() => { IMsgBoxService.ShowMessage(msg, "警告", MessageButton.OK, MessageIcon.Warning); }); } /// /// 显示错误消息 /// /// public void ShowError(string msg) { Invoke(() => { IMsgBoxService.ShowMessage(msg, "错误", MessageButton.OK, MessageIcon.Error); }); } /// /// 显示确认消息 /// /// /// public bool ShowConfirm(string msg) { bool res = false; Invoke(() => { if (!msg.Contains("?") && !msg.Contains("?")) msg += "?"; res = IMsgBoxService.ShowMessage(msg, "确认", MessageButton.YesNo, MessageIcon.Question) == MessageResult.Yes; }); return res; } /// /// 显示Overlay窗体 /// public void ShowOverlayForm() { Invoke(IOverlayService.ShowOverlayForm); } /// /// 关闭Overlay窗体 /// public void CloseOverlayForm() { Invoke(IOverlayService.CloseOverlayForm); } /// /// 显示SplashScreen等待窗体 /// /// public void ShowWaitForm(string caption) { IScreenService.SetSplashScreenState(new string[] { "请稍后", caption }); IScreenService.ShowSplashScreen("MyWaitForm"); } /// /// 关闭SplashScreen等待窗体 /// public void CloseWaitForm() { Invoke(IScreenService.HideSplashScreen); } /// /// 显示ViewModel对应的View /// public void ShowCurrentView() { ChildViewCancel = true; Invoke(ICurrentWindowService.Show); } /// /// 隐藏ViewModel对应的View /// public void HideCurrentView() { Invoke(ICurrentWindowService.Hide); } /// /// 子窗体关闭时是否为取消操作状态.默认true /// public bool ChildViewCancel { get; set; } = true; /// /// 窗体是否为取消状态.默认true /// public bool IsCancel { get; set; } = true; /// /// 关闭ViewModel对应的View /// public void CloseCurrentView() { Invoke(ICurrentWindowService.Close); var pVm = this.GetParentViewModel(); pVm.ChildViewCancel = IsCancel; } /// /// 显示指定的View /// public void ShowView(string view, string title, object parameter) { ChildViewCancel = true; IWindowService.Title = title; IWindowService.Show(view, parameter, this); } /// /// 隐藏ShowView指定的View /// public void HideView() { IWindowService.Hide(); } /// /// 关闭ShowView指定的View /// public void CloseView() { IWindowService.Title = "UserClosed"; IWindowService.Close(); var pVm = this.GetParentViewModel(); pVm.ChildViewCancel = IsCancel; } public MessageResult ShowDialogView(string view, string title, MessageButton btn = MessageButton.OKCancel) { return IDialogService.ShowDialog(MessageButton.OKCancel, title, view, this); } #region service IDispatcherService IDispatchService => this.GetService(); INotificationService INotifyService => this.GetService(); IMessageBoxService IMsgBoxService => this.GetService(); IOverlayFormService IOverlayService => this.GetService(); ISplashScreenService IScreenService => this.GetService(); IFolderBrowserDialogService IFolderService => this.GetService(); ISaveFileDialogService ISaveFileService => this.GetService(); IOpenFileDialogService IOpenFileService => this.GetService(); ICurrentWindowService ICurrentWindowService => this.GetService(); IWindowService IWindowService => this.GetService(); IDialogService IDialogService => this.GetService(); #endregion }