public MessageBoxViewModel( TRMessageBoxWindow view, string title, string message, MessageBoxButton buttonOption, MessageBoxImage image, MessageBoxResult defaultResult, MessageBoxOptions options) { //TextAlignment Title = title; Message = message; ButtonOption = buttonOption; Options = options; SetDirections(options); SetButtonVisibility(buttonOption); SetImageSource(image); //Pen-Added ImageVisibility = Visibility.Visible; if (image == MessageBoxImage.None) { ImageVisibility = Visibility.Collapsed; } SetButtonDefault(defaultResult); _view = view; }
//public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options) //{ // return ShowCore(null, messageBoxText, caption, button, icon, defaultResult, options); //} //public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult) //{ // return ShowCore(owner, messageBoxText, caption, button, icon, defaultResult); //} //public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options) //{ // return ShowCore(owner, messageBoxText, caption, button, icon, defaultResult, options); //} private static MessageBoxResult ShowCore( Window owner, string messageBoxText, string caption = "TR7007i", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None, bool forceShowMessageBoxInUIThread = false) { try { //Log.Info("Enter TRMessageBox.ShowCore({0})", messageBoxText); MessageBoxResult result = MessageBoxResult.None; if (Application.Current.Dispatcher.CheckAccess()) //the calling thread is the thread associated with this Dispatcher { result = TRMessageBoxWindow.Show(owner, messageBoxText, caption, button, icon, defaultResult, options); } else if (forceShowMessageBoxInUIThread) { return((MessageBoxResult)Agent.AppInvoke( new System.Func <MessageBoxResult>(() => { return TRMessageBoxWindow.Show(owner, messageBoxText, caption, button, icon, defaultResult, options); }))); } else { //PenNote: 如果在Task裡面使用下面的 Invoke,會和 Task.Wait()造成DeadLock, // Multi-Thread也同樣有此潛在問題,故用MessageBox取代Invoke,以防萬一 result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options); } //string msg = messageBoxText.Replace("\r\n", " "); //msg = msg.Replace("\n", ""); //L4.msg.InfoFormat("{0},{1},{2},{3}", Path.GetFileName(Assembly.GetEntryAssembly().Location), // caption, msg.Trim(), result.ToString()); return(result); } catch (Exception ex) //If a window has never shown, assign MsgBox owner to it throws an exception. { //Log.Info("Exception catched in TRMessageBox.ShowCore(...). Reason = {0}", ex.Message); return(MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options)); } finally { //Log.Info("Leave TRMessageBox.ShowCore(...)"); } }
public static MessageBoxResult Show( Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options) { if ((options & MessageBoxOptions.DefaultDesktopOnly) == MessageBoxOptions.DefaultDesktopOnly) { throw new NotImplementedException(); } if ((options & MessageBoxOptions.ServiceNotification) == MessageBoxOptions.ServiceNotification) { throw new NotImplementedException(); } //加這行的原因,請見下面的PenNote註解 Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.ContextIdle, new Action(() => { })); //PenNote: 因為_msgWindow是static,以下的psudo code會造成在第二個TRMessageBox中的dispatcher queue裡面, // 叫出第一個BeginInvoke裡面的TRMessageBox。而第一個TRMessageBox的結果,會覆蓋掉第二個的結果, // (因為static物件被改掉了, see _msgWindow = new TRMessageBoxWindow()),因而產生bug。 //public static void DoAbout() //--> to test, put it in CoreCommand.cs //{ // Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => // { // TRMessageBox.Show("1:OnlyShow"); // })); // var result = TRMessageBox.Show("2:YesOrNo", "caption", MessageBoxButton.YesNo); // return; //} _msgWindow = new TRMessageBoxWindow(); if (Application.Current.MainWindow == _msgWindow) { Application.Current.MainWindow = null; //不這樣設,之後的MainWindow秀不出來。(Test case: App.xaml.cs在create MainWindow之前,呼叫TRMessageBox) } if (owner == null) { owner = Application.Current.MainWindow; } try { _msgWindow.Owner = owner; //需避免把owner指定為自己,會crash。(尤其自己是MainWindow的時候,在前面已防掉) } catch (Exception ex) //e.g. assign owner to a closed window. { //Log.Info("Exception catched in TRMessageBoxWindow.Show() #_msgWindow.Owner = owner #. Reason = {0}", ex.Message); _msgWindow.Owner = Application.Current.MainWindow; } _msgWindow._viewModel = new MessageBoxViewModel(_msgWindow, caption, messageBoxText, button, icon, defaultResult, options); _msgWindow.DataContext = _msgWindow._viewModel; _msgWindow.Topmost = true; //always set it to topmost, otherwise if may obstacled by other topmost window. prevCursor = null; try { prevCursor = Mouse.OverrideCursor; Mouse.OverrideCursor = Cursors.Arrow; } catch (Exception ex) { //Log.Error("Exception catched in TRMessageBoxWindow.Show() on setting cursor. Reason={0}", ex.Message); } _msgWindow.ShowDialog(); try { Mouse.OverrideCursor = prevCursor; } catch (Exception ex) { //Log.Error("Exception catched in TRMessageBoxWindow.Show() on setting cursor. Reason={0}", ex.Message); } return(_msgWindow._viewModel.Result); }