NewMessageBox.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Windows.Input;
  2. using Muchinfo.PC.Common.Helpers;
  3. using System;
  4. using System.Windows;
  5. using System.Windows.Interop;
  6. using System.Windows.Media;
  7. namespace Muchinfo.MTPClient.Infrastructure.MessageBox
  8. {
  9. /// <summary>
  10. /// NewMessageBox.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class NewMessageBox : Window
  13. {
  14. private MessageBoxResult _dialogResult;
  15. private MessageBoxButton _button;
  16. private bool _flag = false;
  17. public NewMessageBox()
  18. {
  19. InitializeComponent();
  20. this.Loaded += NewMessageBox_Loaded;
  21. }
  22. void NewMessageBox_Loaded(object sender, RoutedEventArgs e)
  23. {
  24. this.btnOK.Focus();
  25. }
  26. public MessageBoxResult MessageBoxResult
  27. {
  28. get
  29. {
  30. return this._dialogResult;
  31. }
  32. }
  33. public static MessageBoxResult Show(Window owner, string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, Brush background)
  34. {
  35. return NewMessageBox.ShowCore(owner, messageText, caption, button, icon, MessageBoxResult.None, null, background);
  36. }
  37. protected void InitializeMessageBox(Window owner, string text, string caption, MessageBoxButton button, MessageBoxImage image, MessageBoxResult defaultResult)
  38. {
  39. this.Owner = owner;
  40. MessageText.Text = text;
  41. this.Title = caption;
  42. _button = button;
  43. switch (image)
  44. {
  45. case MessageBoxImage.Question:
  46. MessageBoxImageButton.Source = ResourceHelper.GetFromeResource<ImageSource>("MessageBoxQuestion");
  47. break;
  48. case MessageBoxImage.Warning:
  49. MessageBoxImageButton.Source = ResourceHelper.GetFromeResource<ImageSource>("MessageBoxWarning");
  50. break;
  51. default:
  52. MessageBoxImageButton.Source = ResourceHelper.GetFromeResource<ImageSource>("MessageBoxInformation");
  53. break;
  54. }
  55. switch (button)
  56. {
  57. case MessageBoxButton.OK:
  58. btnCancel.Visibility = Visibility.Collapsed;
  59. break;
  60. case MessageBoxButton.OKCancel:
  61. case MessageBoxButton.YesNo:
  62. btnCancel.Visibility = Visibility.Visible;
  63. break;
  64. }
  65. //_defaultResult = defaultResult;
  66. }
  67. private static MessageBoxResult ShowCore(Window owner, string messageText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, Style messageBoxStyle, Brush background)
  68. {
  69. if (BrowserInteropHelper.IsBrowserHosted)
  70. {
  71. throw new InvalidOperationException("Static methods for MessageBoxes are not available in XBAP. Use the instance ShowMessageBox methods instead.");
  72. }
  73. var messageBox = new NewMessageBox();
  74. messageBox.InitializeMessageBox(owner, messageText, caption, button, icon, defaultResult);
  75. if (messageBoxStyle != null)
  76. {
  77. messageBox.Style = messageBoxStyle;
  78. }
  79. //messageBox.Width = 300;
  80. //messageBox.Height = 200;
  81. messageBox.Background = background;
  82. messageBox.ShowDialog();
  83. return messageBox.MessageBoxResult;
  84. //MessageBox messageBox = new MessageBox();
  85. //messageBox.InitializeMessageBox(owner, messageText, caption, button, icon, defaultResult);
  86. //if (messageBoxStyle != null)
  87. //{
  88. // messageBox.Style = messageBoxStyle;
  89. //}
  90. //messageBox.ShowDialog();
  91. //return messageBox.MessageBoxResult;
  92. }
  93. private void BtnOK_OnClick(object sender, RoutedEventArgs e)
  94. {
  95. //_dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.OK : MessageBoxResult.Yes;
  96. //_flag = true;
  97. //this.Close();
  98. //e.Handled = true;
  99. }
  100. private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
  101. {
  102. _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.Cancel : MessageBoxResult.No;
  103. _flag = true;
  104. this.Close();
  105. }
  106. private void NewMessageBox_OnClosed(object sender, EventArgs e)
  107. {
  108. if (_flag)
  109. {
  110. _flag = false;
  111. }
  112. else
  113. {
  114. _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.Cancel : MessageBoxResult.No;
  115. }
  116. this.Close();
  117. }
  118. private void BtnOK_OnKeyUp(object sender, KeyEventArgs e)
  119. {
  120. if (e.Key == Key.Enter)
  121. {
  122. _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.OK : MessageBoxResult.Yes;
  123. _flag = true;
  124. e.Handled = true;
  125. this.DialogResult = true;
  126. }
  127. }
  128. private void BtnOK_OnMouseDown(object sender, MouseButtonEventArgs e)
  129. {
  130. _dialogResult = (_button == MessageBoxButton.OKCancel || _button == MessageBoxButton.OK) ? MessageBoxResult.OK : MessageBoxResult.Yes;
  131. _flag = true;
  132. this.Close();
  133. e.Handled = true;
  134. }
  135. }
  136. }