using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.ComponentModel; using System.Windows.Input; using System.Windows.Documents; using System.Threading; using System.Diagnostics; using GummingCommon; namespace GummingCommon { public class MessageBoxServiceViewModule : IMessageBoxService, INotifyPropertyChanged { public MessageBoxServiceView View { get; set; } public bool IsKeepInProcessVisibility { get; set; } #region constructor public MessageBoxServiceViewModule() { CancelCommand = new DelegateCommand(this.btCancelX_Click); IsKeepInProcessVisibility = false; View = new MessageBoxServiceView(); View.DataContext = this; WindowTitle = ""; View.Topmost = true; } public MessageBoxServiceViewModule(string lm, string cm, string rm) { this.btLeftContent = lm; this.btCenterContent = cm; this.btRightContent = rm; CancelCommand = new DelegateCommand(this.btCancelX_Click); IsKeepInProcessVisibility = false; View = new MessageBoxServiceView(); View.DataContext = this; WindowTitle = ""; View.Topmost = true; } #endregion #region destructor ~MessageBoxServiceViewModule() { _self = null; } #endregion #region Binding Properties public string WindowTitle { get; private set; } private Visibility _spWarningVisibility; public Visibility spWarningVisibility { get { return _spWarningVisibility; } set { _spWarningVisibility = value; OnPropertyChanged("spWarningVisibility"); } } private Visibility _spErrorVisibility; public Visibility spErrorVisibility { get { return _spErrorVisibility; } set { _spErrorVisibility = value; OnPropertyChanged("spErrorVisibility"); } } private Visibility _spInforVisibility; public Visibility spInforVisibility { get { return _spInforVisibility; } set { _spInforVisibility = value; OnPropertyChanged("spInforVisibility"); } } private Visibility _btLeftVisibility; public Visibility btLeftVisibility { get { return _btLeftVisibility; } set { _btLeftVisibility = value; OnPropertyChanged("btLeftVisibility"); } } private Visibility _btCenterVisibility; public Visibility btCenterVisibility { get { return _btCenterVisibility; } set { _btCenterVisibility = value; OnPropertyChanged("btCenterVisibility"); } } private Visibility _btRightVisibility; public Visibility btRightVisibility { get { return _btRightVisibility; } set { _btRightVisibility = value; OnPropertyChanged("btRightVisibility"); } } private object _btLeftContent; public object btLeftContent { get { return _btLeftContent; } set { _btLeftContent = value; OnPropertyChanged("btLeftContent"); } } private object _btCenterContent; public object btCenterContent { get { return _btCenterContent; } set { _btCenterContent = value; OnPropertyChanged("btCenterContent"); } } private object _btRightContent; public object btRightContent { get { return _btRightContent; } set { _btRightContent = value; OnPropertyChanged("btRightContent"); } } /// /// /// public bool Topmost { get; set; } /// /// /// private string _Message; public string Message { get { return _Message; } set { _Message = value; OnPropertyChanged("Message"); } } private bool _LeftButtonIsDefault; public bool LeftButtonIsDefault { get { return _LeftButtonIsDefault; } set { _LeftButtonIsDefault = value; OnPropertyChanged("LeftButtonIsDefault"); } } private bool _CenterButtonIsDefault; public bool CenterButtonIsDefault { get { return _CenterButtonIsDefault; } set { _CenterButtonIsDefault = value; OnPropertyChanged("CenterButtonIsDefault"); } } private bool _RightButtonIsDefault; public bool RightButtonIsDefault { get { return _RightButtonIsDefault; } set { _RightButtonIsDefault = value; OnPropertyChanged("RightButtonIsDefault"); } } #endregion #region Binding Commands private ICommand _HyperLinkClicked; public ICommand HyperLinkClicked { get { return _HyperLinkClicked; } set { _HyperLinkClicked = value; OnPropertyChanged("HyperLinkClicked"); } } private ICommand _CancelCommand; public ICommand CancelCommand { get { return _CancelCommand; } set { _CancelCommand = value; OnPropertyChanged("CancelCommand"); } } private ICommand _LeftButtonCommand; public ICommand LeftButtonCommand { get { return this._LeftButtonCommand; } set { this._LeftButtonCommand = value; OnPropertyChanged("LeftButtonCommand"); } } private ICommand _CenterButtonCommand; public ICommand CenterButtonCommand { get { return this._CenterButtonCommand; } set { this._CenterButtonCommand = value; OnPropertyChanged("CenterButtonCommand"); } } private ICommand _RightButtonCommand; public ICommand RightButtonCommand { get { return this._RightButtonCommand; } set { this._RightButtonCommand = value; OnPropertyChanged("RightButtonCommand"); } } #endregion #region IMessageBoxService members private void ClearStatusDefaultCancel() { _LeftButtonIsDefault = false; _CenterButtonIsDefault = false; _RightButtonIsDefault = false; } /// /// Gets or sets the state of the dialog. /// /// The state of the dialog. public DialogState DialogState { get; set; } /// /// Gets or sets the value of the CanMuchOpened. /// public bool CanMuchOpened { get; set; } /// /// Displays an error dialog with a given message. /// /// The message to be displayed. public void ShowError(string message, Window owner) { View.Owner = owner; DialogState = DialogState.Cancel; ShowMessage(message, "Error", CustomDialogIcons.Error); } public void ShowError(string message) { DialogState = DialogState.Cancel; ShowMessage(message, "Error", CustomDialogIcons.Error); } /// /// Displays an error dialog with a given message. /// /// The message to be displayed. public void ShowInformation(string message) { DialogState = DialogState.Cancel; ShowMessage(message, "Information", CustomDialogIcons.Information); } /// /// Shows an Successful message /// /// The Success Message public void ShowSuccess(string message) { DialogState = DialogState.Cancel; ShowMessage(message, "Successful", CustomDialogIcons.OK); } /// /// Displays an error dialog with a given message. /// /// The message to be displayed. public void ShowWarning(string message) { DialogState = DialogState.Cancel; ShowMessage(message, "Warning", CustomDialogIcons.Warning); } /// /// Displays a Yes/No dialog and returns the user input. /// /// The message to be displayed. /// The icon to be displayed. /// User selection. public void ShowYesNo(string message, CustomDialogIcons icon) { DialogState = DialogState.Cancel; this.Message = message; SetImage(icon); this.btLeftVisibility = Visibility.Hidden; this.btCenterVisibility = Visibility.Visible; this.btRightVisibility = Visibility.Visible; this.btCenterContent = "确定"; this.btRightContent = "返回"; //this.btCenter.Focus(); ClearStatusDefaultCancel(); CenterButtonIsDefault = true; this.CenterButtonCommand = new DelegateCommand(btOk_Click); this.RightButtonCommand = new DelegateCommand(btNo_Click); View.ShowDialog(); } /// /// Displays a Yes/No/Cancel dialog and returns the user input. /// /// The message to be displayed. /// The icon to be displayed. /// User selection. public void ShowYesNoCancel(string message, CustomDialogIcons icon) { DialogState = DialogState.Cancel; this.Message = message; SetImage(icon); this.btLeftVisibility = Visibility.Visible; this.btCenterVisibility = Visibility.Visible; this.btRightVisibility = Visibility.Visible; if (this.btLeftContent == null) { this.btLeftContent = "确定"; this.btCenterContent = "返回"; this.btRightContent = "取消"; } ClearStatusDefaultCancel(); LeftButtonIsDefault = true; this.LeftButtonCommand = new DelegateCommand(btOk_Click); this.CenterButtonCommand = new DelegateCommand(btNo_Click); this.RightButtonCommand = new DelegateCommand(btCancel_Click); View.ShowDialog(); } /// /// Displays a OK/Cancel dialog and returns the user input. /// /// The message to be displayed. /// The icon to be displayed. /// User selection. public void ShowOkCancel(string message, CustomDialogIcons icon) { DialogState = DialogState.Cancel; this.Message = message; SetImage(icon); this.btLeftVisibility = Visibility.Hidden; this.btCenterVisibility = Visibility.Visible; this.btRightVisibility = Visibility.Visible; this.btCenterContent = "确定"; this.btRightContent = "取消"; //this.btCenter.Focus(); ClearStatusDefaultCancel(); CenterButtonIsDefault = true; this.CenterButtonCommand = new DelegateCommand(btOk_Click); this.RightButtonCommand = new DelegateCommand(btNo_Click); View.ShowDialog(); } private IMessageBoxService _self; public IMessageBoxService New() { _self = new MessageBoxServiceViewModule(); DialogState = DialogState.Cancel; return _self; } public bool ShowInTaskBar { get { return View.ShowInTaskbar; } set { View.ShowInTaskbar = value; } } #endregion #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged = delegate { }; private void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion #region Private Methods /// /// Shows a standard System.Windows.MessageBox using the parameters requested /// /// The message to be displayed. /// The heading to be displayed /// The icon to be displayed. private void ShowMessage(string message, string heading, CustomDialogIcons icon) { this.Message = message; SetImage(icon); this.btLeftVisibility = Visibility.Hidden; this.btCenterVisibility = Visibility.Hidden; this.btRightVisibility = Visibility.Visible; this.btRightContent = "确定"; //this.btRight.Focus(); ClearStatusDefaultCancel(); RightButtonIsDefault = true; View.Topmost = Topmost; this.RightButtonCommand = new DelegateCommand(btOk_Click); View.ShowDialog(); } /// /// Translates a CustomDialogIcons into a standard WPF System.Windows.MessageBox MessageBoxImage. /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The icon to be displayed. /// A standard WPF System.Windows.MessageBox MessageBoxImage private void SetImage(CustomDialogIcons icon) { switch (icon) { case CustomDialogIcons.Information: this.spWarningVisibility = Visibility.Hidden; this.spErrorVisibility = Visibility.Hidden; this.spInforVisibility = Visibility.Visible; break; case CustomDialogIcons.Question: this.spWarningVisibility = Visibility.Hidden; this.spErrorVisibility = Visibility.Hidden; this.spInforVisibility = Visibility.Visible; break; case CustomDialogIcons.Exclamation: this.spWarningVisibility = Visibility.Hidden; this.spErrorVisibility = Visibility.Hidden; this.spInforVisibility = Visibility.Visible; break; case CustomDialogIcons.Stop: this.spWarningVisibility = Visibility.Visible; this.spErrorVisibility = Visibility.Hidden; this.spInforVisibility = Visibility.Hidden; break; case CustomDialogIcons.Warning: this.spWarningVisibility = Visibility.Visible; this.spErrorVisibility = Visibility.Hidden; this.spInforVisibility = Visibility.Hidden; break; case CustomDialogIcons.Error: this.spWarningVisibility = Visibility.Hidden; this.spErrorVisibility = Visibility.Visible; this.spInforVisibility = Visibility.Hidden; break; case CustomDialogIcons.OK: this.spWarningVisibility = Visibility.Hidden; this.spErrorVisibility = Visibility.Hidden; this.spInforVisibility = Visibility.Visible; break; } } private void btOk_Click(object sender) { DialogState = DialogState.Ok; View.DialogResult = true; } private void btNo_Click(object sender) { DialogState = DialogState.No; View.DialogResult = true; } private void btCancel_Click(object sender) { DialogState = DialogState.Cancel; View.DialogResult = true; } private void btCancelX_Click(object sender) { DialogState = DialogState.CancelX; View.DialogResult = true; } #endregion } }