/// <summary> /// Display a message-box to the user, based upon the given options. /// This particular method is the one that all of the other Notify methods invoke to do the actual work. /// </summary> /// <param name="options">a JhMessageBoxOptions object that fully specifies everything about the message-box</param> /// <returns>a JhDialogResult indicating which action the user took, or TimedOut if the user took no action before the timeout expired</returns> public JhDialogResult NotifyUser(JhMessageBoxOptions options) { var messageBoxWindow = new JhMessageBoxWindow(this, options); // If we are doing testing, and have specified that a specific button is to be emulated as having been selected by the user, // then verify that that button is indeed present.. if (IsTesting) { if (TestFacility.ButtonResultToSelect.HasValue) { string complaint; if (!GetWhetherButtonIsIncluded(options.ButtonFlags, out complaint)) { // Announce the error throw new ArgumentOutOfRangeException(complaint); } } } //TODO: The following should not be needed for Silverlight.. // Try to give it a parent-window to position itself relative to. #if !SILVERLIGHT FrameworkElement parentElement = options.ParentElement; Window parentWindow = null; if (parentElement == null) { // When running unit-tests, Application.Current might be null. if (Application.Current != null) { parentWindow = Application.Current.MainWindow; messageBoxWindow.Owner = parentWindow; } } else if (parentElement.IsLoaded) { parentWindow = parentElement as Window; if (parentWindow != null) { messageBoxWindow.Owner = parentWindow; } } #endif // Finally, show the message-box. //TODO: for Silverlight, need to prepare to receive the result asynchronously!!! #if SILVERLIGHT messageBox.Show(); #else _messageBoxWindow = messageBoxWindow; if (options.IsAsynchronous) { messageBoxWindow.Show(); } else { messageBoxWindow.ShowDialog(); } #endif return messageBoxWindow.Result; }
/// <summary> /// Handle the ContentRendered event, which occurrs after the UI elements have been rendered on-screen. /// </summary> void OnContentRendered(object sender, EventArgs e) { #region Make me the top-most window. if (!this.IsTesting) { if (_options.IsToBeTopmostWindow) { #if !SILVERLIGHT this.Topmost = true; this.Focus(); #endif } } #endregion #region Audial effects if (!this.IsTesting && _options.IsSoundEnabled) { if (!_options.IsUsingNewerSoundScheme) { JhMessageBoxWindow.MessageBeep(_options.MessageType); } else { //TODO: How to implement this in SL? #if !SILVERLIGHT switch (_options.MessageType) { case JhMessageBoxType.Information: AudioLib.PlaySoundResourceEvenIfNotPCM(_informationSoundResource); break; case JhMessageBoxType.Question: AudioLib.PlaySoundResourceEvenIfNotPCM(_questionSoundResource); break; case JhMessageBoxType.UserMistake: AudioLib.PlaySoundResource(_userMistakeSoundResource); break; case JhMessageBoxType.Warning: AudioLib.PlaySoundResource(_warningSoundResource); break; case JhMessageBoxType.Error: AudioLib.PlaySoundResource(_errorSoundResource); break; case JhMessageBoxType.SecurityIssue: AudioLib.PlaySoundResource(_securityIssueSoundResource); break; case JhMessageBoxType.Stop: AudioLib.PlaySoundResource(_stopSoundResource); break; } #endif } } #endregion Audial effects #region Timeout if (!IsTesting || (IsTesting && !TestFacility.IsToWaitForExplicitClose)) { // Set the timer that will close this message-box. int timeoutPeriodToSet = _options.TimeoutPeriodInSeconds; if (timeoutPeriodToSet == 0) { // If the default value of zero was indicated, that means to use the default value. timeoutPeriodToSet = JhMessageBoxOptions.GetDefaultTimeoutValueFor(_options.MessageType); _options.TimeoutPeriodInSeconds = timeoutPeriodToSet; } // If we are running tests and this option is set -- limit the timeout to our special, brief time-period. if (_manager._testFacility != null && TestFacility.IsTimeoutsFast) { if (timeoutPeriodToSet > 1) { timeoutPeriodToSet = 1; } } StartTimer(timeoutPeriodToSet); } #endregion Timeout }
/// <summary> /// The constructor /// </summary> /// <param name="result">the JhDialogResult that the message-box was ended with</param> /// <param name="error">an exception if it was thrown during the operation of the message-box, otherwise null</param> /// <param name="messageBoxWindow">the message-box that raised this event</param> public MessageBoxCompletedArgs(JhDialogResult result, Exception error, JhMessageBoxWindow messageBoxWindow) : base(error: error, cancelled: false, userState: messageBoxWindow) { this.Result = result; this.MessageBoxWindow = messageBoxWindow; this.Options = messageBoxWindow._options; }
/// <summary> /// Raise the Completed event with parameters indicating the current message-box window and the user-response /// it was closed with. /// </summary> /// <remarks> /// This is attached to JhMessageBoxOptions because it's handler needs to exist at the instance-level, /// and the Options object is easily available to the developer. /// </remarks> /// <param name="messageBoxWindow">the message-box window that signaled this event</param> /// <param name="result">the user-response with which the message-box window was dismissed</param> public void SignalThatMessageBoxHasEnded(JhMessageBoxWindow messageBoxWindow, JhDialogResult result) { var args = new MessageBoxCompletedArgs(result: result, error: null, messageBoxWindow: messageBoxWindow); this.Completed(this, args); }