using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using System.Threading; using System.Windows.Controls; using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using System.Reflection; namespace GummingCommon { public class ViewModelBase : INotifyPropertyChanged, IDisposable { public delegate void ResponseCallbackDelegate(ResopnseType type, object content); public virtual event ResponseCallbackDelegate OnResponseCallbackDelegate; public bool IsKeepInProcessVisibility { get; set; } public bool DisabledShowDialogView { get; set; } public bool IsActivePage { get; set; } public bool IsActiveChildPage { get; set; } public UIElement SourceElement { get; set; } public ViewModelBase() { IsKeepInProcessVisibility = false; DisabledShowDialogView = false; } public virtual void InitializeParameters(object content = null) { } protected void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } protected void OnPropertyChanged(PropertyInfo[] propertyNames) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { foreach (var s in propertyNames) handler(this, new PropertyChangedEventArgs(s.Name)); } } public event PropertyChangedEventHandler PropertyChanged; public void OnMainPageCallbackDelegate(CallbackType calltype, Object content) { AppEventAggregator.GetEventAggregator().GetEvent().Publish(new NotifyEventParameter() { NotifyType = calltype, NotifyContent= content }); } public void OnCallbackPreviousPage() { if (AppParameter.CommandList.Count > 1) { var viewmodule = AppParameter.CommandList[AppParameter.CommandList.Count - 2]; AppParameter.CommandList.RemoveAt(AppParameter.CommandList.Count - 1);//remove last AppParameter.CommandList.RemoveAt(AppParameter.CommandList.Count - 1);//remove next OnMainPageCallbackDelegate(CallbackType.PreviousView, viewmodule); } } public virtual void OnGotoTopLevel() { OnCallbackPreviousPage(); } public void Dispose() { OnDispose(); } protected virtual void OnDispose() { } public void ShowProgressView(string message) { Application.Current.Dispatcher.Invoke((Action)(() => { OnMainPageCallbackDelegate(CallbackType.StartProcessing, message); })); } public void SetProgressView(string message) { Application.Current.Dispatcher.Invoke((Action)(() => { OnMainPageCallbackDelegate(CallbackType.InProcessing, message); })); } public void CloseProgressView() { try { if (Application.Current != null) { Application.Current.Dispatcher.Invoke((Action)(() => { OnMainPageCallbackDelegate(CallbackType.EndProcess, null); })); } } catch { }//不需要捕获异常 } public void ShowMessageBox(string message) { AppEventAggregator.GetEventAggregator().GetEvent().Publish(new NotifyEventParameter() { NotifyType = CallbackType.KeyPressThreadStop }); CloseProgressView(); if (Application.Current != null) { Application.Current.Dispatcher.Invoke((Action)(() => { Helpering.ShowMessageBox(message); })); } } public void ShowErrorBox(string message, Window owner) { CloseProgressView(); if (Application.Current != null) { Application.Current.Dispatcher.Invoke((Action)(() => { Helpering.ShowErrorBox(message, owner); })); } } public void ShowErrorBox(string message) { CloseProgressView(); if (Application.Current != null) { Application.Current.Dispatcher.Invoke((Action)(() => { Helpering.ShowErrorBox(message); })); } } public void ShowErrorBox(List errors, List warnings) { CloseProgressView(); if (Application.Current != null) { Application.Current.Dispatcher.Invoke((Action)(() => { string message = string.Empty; foreach (string error in errors) { message += error + Environment.NewLine; } foreach (string warning in warnings) { message += warning + Environment.NewLine; } Helpering.ShowErrorBox(message); })); } } public void ShowWarningBox(string message) { CloseProgressView(); if (Application.Current != null) { Application.Current.Dispatcher.Invoke((Action)(() => { Helpering.ShowWarningBox(message); })); } } public void ShowWarningBoxWithoutCloseProgress(string message) { if (Application.Current != null) { Application.Current.Dispatcher.Invoke((Action)(() => { IMessageBoxService confirm = new MessageBoxServiceViewModule(); confirm.IsKeepInProcessVisibility = true; confirm.ShowWarning(message); })); } } protected void OnSendProgressDelegate(object content) { ShowProgressView((string)content); } #region 身份证验证 /// /// 验证身份证号码是否正确 /// /// /// protected bool CheckIdentify(string idCard) { var matchString = ""; if (idCard.Length == 15) { matchString = "^[1-9]\\d{7}((0[1-9])||(1[0-2]))((0[1-9])||(1\\d)||(2\\d)||(3[0-1]))\\d{3}$"; } else if (idCard.Length == 18) { matchString = "^[1-9]\\d{5}[1-9]\\d{3}((0[1-9])||(1[0-2]))((0[1-9])||(1\\d)||(2\\d)||(3[0-1]))\\d{3}([0-9]||X)$"; } if (string.IsNullOrEmpty(matchString)) return false; var m = Regex.Match(idCard, matchString); return m.Success; } protected bool CheckMobileIdentify(string mobile) { var m = Regex.Match(mobile, "^[1][358]\\d{9}$"); return m.Success; } protected string GetBirthday(string idCard) { if (string.IsNullOrEmpty(idCard)) return null; if (!CheckIdentify(idCard)) return null; if (idCard.Length == 15) return "19" + idCard.Substring(6, 6); if (idCard.Length == 18) return idCard.Substring(6, 8); return null; } #endregion } }