| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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
- {
- /// <summary>
- /// NewMessageBox.xaml 的交互逻辑
- /// </summary>
- 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<ImageSource>("MessageBoxQuestion");
- break;
- case MessageBoxImage.Warning:
- MessageBoxImageButton.Source = ResourceHelper.GetFromeResource<ImageSource>("MessageBoxWarning");
- break;
- default:
- MessageBoxImageButton.Source = ResourceHelper.GetFromeResource<ImageSource>("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;
- }
- }
- }
|