using System.Windows.Input; using Muchinfo.PC.Common.Helpers; using System; using System.Windows; using System.Windows.Interop; using System.Windows.Media; namespace Muchinfo.MTPClient.Infrastructure.MessageBox { /// /// NewMessageBox.xaml 的交互逻辑 /// public partial class NewMessageBox : Window { private MessageBoxResult _dialogResult; private MessageBoxButton _button; private bool _flag = false; public NewMessageBox() { InitializeComponent(); this.Loaded += NewMessageBox_Loaded; } void NewMessageBox_Loaded(object sender, RoutedEventArgs e) { this.btnOK.Focus(); } public MessageBoxResult MessageBoxResult { get { return this._dialogResult; } } public static MessageBoxResult Show(Window owner, string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, Brush background) { return NewMessageBox.ShowCore(owner, messageText, caption, button, icon, MessageBoxResult.None, null, background); } protected void InitializeMessageBox(Window owner, string text, string caption, MessageBoxButton button, MessageBoxImage image, MessageBoxResult defaultResult) { this.Owner = owner; MessageText.Text = text; this.Title = caption; _button = button; switch (image) { case MessageBoxImage.Question: MessageBoxImageButton.Source = ResourceHelper.GetFromeResource("MessageBoxQuestion"); break; case MessageBoxImage.Warning: MessageBoxImageButton.Source = ResourceHelper.GetFromeResource("MessageBoxWarning"); break; default: MessageBoxImageButton.Source = ResourceHelper.GetFromeResource("MessageBoxInformation"); break; } switch (button) { case MessageBoxButton.OK: btnCancel.Visibility = Visibility.Collapsed; break; case MessageBoxButton.OKCancel: case MessageBoxButton.YesNo: btnCancel.Visibility = Visibility.Visible; break; } //_defaultResult = defaultResult; } private static MessageBoxResult ShowCore(Window owner, string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, Style messageBoxStyle, Brush background) { if (BrowserInteropHelper.IsBrowserHosted) { throw new InvalidOperationException("Static methods for MessageBoxes are not available in XBAP. Use the instance ShowMessageBox methods instead."); } var messageBox = new NewMessageBox(); messageBox.InitializeMessageBox(owner, messageText, caption, button, icon, defaultResult); if (messageBoxStyle != null) { messageBox.Style = messageBoxStyle; } //messageBox.Width = 300; //messageBox.Height = 200; messageBox.Background = background; messageBox.ShowDialog(); return messageBox.MessageBoxResult; //MessageBox messageBox = new MessageBox(); //messageBox.InitializeMessageBox(owner, messageText, caption, button, icon, defaultResult); //if (messageBoxStyle != null) //{ // messageBox.Style = messageBoxStyle; //} //messageBox.ShowDialog(); //return messageBox.MessageBoxResult; } private void BtnOK_OnClick(object sender, RoutedEventArgs e) { //_dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.OK : MessageBoxResult.Yes; //_flag = true; //this.Close(); //e.Handled = true; } private void BtnCancel_OnClick(object sender, RoutedEventArgs e) { _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.Cancel : MessageBoxResult.No; _flag = true; this.Close(); } private void NewMessageBox_OnClosed(object sender, EventArgs e) { if (_flag) { _flag = false; } else { _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.Cancel : MessageBoxResult.No; } this.Close(); } private void BtnOK_OnKeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.OK : MessageBoxResult.Yes; _flag = true; e.Handled = true; this.DialogResult = true; } } private void BtnOK_OnMouseDown(object sender, MouseButtonEventArgs e) { _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.OK : MessageBoxResult.Yes; _flag = true; this.Close(); e.Handled = true; } } }