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