374 lines
19 KiB
C#
374 lines
19 KiB
C#
|
// -----------------------------------------------------------------------
|
|||
|
// <copyright file="MessageBox.cs">
|
|||
|
// TODO: Update copyright text.
|
|||
|
// </copyright>
|
|||
|
// -----------------------------------------------------------------------
|
|||
|
|
|||
|
namespace Plane.Windows.Messages
|
|||
|
{
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows;
|
|||
|
using System.Windows.Threading;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box.
|
|||
|
/// </summary>
|
|||
|
public static class Alert
|
|||
|
{
|
|||
|
private static Dispatcher _Dispatcher;
|
|||
|
private static Dispatcher Dispatcher
|
|||
|
{
|
|||
|
get { return _Dispatcher ?? (_Dispatcher = Application.Current.Dispatcher); }
|
|||
|
}
|
|||
|
|
|||
|
private static MessageBoxResult RunOnUIThread(Func<MessageBoxResult> func)
|
|||
|
{
|
|||
|
if (Dispatcher.CheckAccess())
|
|||
|
{
|
|||
|
return func();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return (MessageBoxResult)Dispatcher.Invoke(func);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message and returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult Show(string messageBoxText)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText);
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message and a title bar caption; and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult Show(string messageBoxText, string caption)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption);
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box in front of the specified window. The message box displays a message and returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="owner">A System.Windows.Window that represents the owner window of the message box.</param>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult Show(Window owner, string messageBoxText)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText);
|
|||
|
msg.Owner = owner;
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box in front of the specified window. The message box displays a message and title bar caption; and it returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="owner">A System.Windows.Window that represents the owner window of the message box.</param>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption);
|
|||
|
msg.Owner = owner;
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, title bar caption, and button; and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="button">A System.Windows.MessageBoxButton value that specifies which button or buttons to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, button);
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, title bar caption, button, and icon; and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="button">A System.Windows.MessageBoxButton value that specifies which button or buttons to display.</param>
|
|||
|
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, button, icon);
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, title bar caption, and OK button with a custom System.String value for the button's text; and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="okButtonText">A System.String that specifies the text to display within the OK button.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowOK(string messageBoxText, string caption, string okButtonText)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OK);
|
|||
|
msg.OkButtonText = okButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, title bar caption, OK button with a custom System.String value for the button's text, and icon; and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="okButtonText">A System.String that specifies the text to display within the OK button.</param>
|
|||
|
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowOK(string messageBoxText, string caption, string okButtonText, MessageBoxImage icon)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OK, icon);
|
|||
|
msg.OkButtonText = okButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, caption, and OK/Cancel buttons with custom System.String values for the buttons' text;
|
|||
|
/// and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="okButtonText">A System.String that specifies the text to display within the OK button.</param>
|
|||
|
/// <param name="cancelButtonText">A System.String that specifies the text to display within the Cancel button.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowOKCancel(string messageBoxText, string caption, string okButtonText, string cancelButtonText)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OKCancel);
|
|||
|
msg.OkButtonText = okButtonText;
|
|||
|
msg.CancelButtonText = cancelButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, caption, OK/Cancel buttons with custom System.String values for the buttons' text, and icon;
|
|||
|
/// and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="okButtonText">A System.String that specifies the text to display within the OK button.</param>
|
|||
|
/// <param name="cancelButtonText">A System.String that specifies the text to display within the Cancel button.</param>
|
|||
|
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowOKCancel(string messageBoxText, string caption, string okButtonText, string cancelButtonText, MessageBoxImage icon)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OKCancel, icon);
|
|||
|
msg.OkButtonText = okButtonText;
|
|||
|
msg.CancelButtonText = cancelButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, caption, and Yes/No buttons with custom System.String values for the buttons' text;
|
|||
|
/// and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="yesButtonText">A System.String that specifies the text to display within the Yes button.</param>
|
|||
|
/// <param name="noButtonText">A System.String that specifies the text to display within the No button.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowYesNo(string messageBoxText, string caption, string yesButtonText, string noButtonText)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNo);
|
|||
|
msg.YesButtonText = yesButtonText;
|
|||
|
msg.NoButtonText = noButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, caption, Yes/No buttons with custom System.String values for the buttons' text, and icon;
|
|||
|
/// and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="yesButtonText">A System.String that specifies the text to display within the Yes button.</param>
|
|||
|
/// <param name="noButtonText">A System.String that specifies the text to display within the No button.</param>
|
|||
|
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowYesNo(string messageBoxText, string caption, string yesButtonText, string noButtonText, MessageBoxImage icon)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNo, icon);
|
|||
|
msg.YesButtonText = yesButtonText;
|
|||
|
msg.NoButtonText = noButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, caption, and Yes/No/Cancel buttons with custom System.String values for the buttons' text;
|
|||
|
/// and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="yesButtonText">A System.String that specifies the text to display within the Yes button.</param>
|
|||
|
/// <param name="noButtonText">A System.String that specifies the text to display within the No button.</param>
|
|||
|
/// <param name="cancelButtonText">A System.String that specifies the text to display within the Cancel button.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowYesNoCancel(string messageBoxText, string caption, string yesButtonText, string noButtonText, string cancelButtonText)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNoCancel);
|
|||
|
msg.YesButtonText = yesButtonText;
|
|||
|
msg.NoButtonText = noButtonText;
|
|||
|
msg.CancelButtonText = cancelButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Displays a message box that has a message, caption, Yes/No/Cancel buttons with custom System.String values for the buttons' text, and icon;
|
|||
|
/// and that returns a result.
|
|||
|
/// </summary>
|
|||
|
/// <param name="messageBoxText">A System.String that specifies the text to display.</param>
|
|||
|
/// <param name="caption">A System.String that specifies the title bar caption to display.</param>
|
|||
|
/// <param name="yesButtonText">A System.String that specifies the text to display within the Yes button.</param>
|
|||
|
/// <param name="noButtonText">A System.String that specifies the text to display within the No button.</param>
|
|||
|
/// <param name="cancelButtonText">A System.String that specifies the text to display within the Cancel button.</param>
|
|||
|
/// <param name="icon">A System.Windows.MessageBoxImage value that specifies the icon to display.</param>
|
|||
|
/// <returns>A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.</returns>
|
|||
|
public static MessageBoxResult ShowYesNoCancel(string messageBoxText, string caption, string yesButtonText, string noButtonText, string cancelButtonText, MessageBoxImage icon)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNoCancel, icon);
|
|||
|
msg.YesButtonText = yesButtonText;
|
|||
|
msg.NoButtonText = noButtonText;
|
|||
|
msg.CancelButtonText = cancelButtonText;
|
|||
|
|
|||
|
msg.ShowDialog();
|
|||
|
|
|||
|
return msg.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
public static MessageBoxResult ShowProgressAutoClose(string loadingText, Func<bool> completedChecker, Func<bool> cancelledChecker)
|
|||
|
{
|
|||
|
return ShowProgressCore(loadingText, msgWindow => msgWindow.Close(), completedChecker, cancelledChecker);
|
|||
|
}
|
|||
|
|
|||
|
public static MessageBoxResult ShowProgress(string loadingText, string completedText, Func<bool> completedChecker, Func<bool> cancelledChecker)
|
|||
|
{
|
|||
|
return ShowProgressCore(loadingText, msgWindow => msgWindow.CompleteProgress(completedText), completedChecker, cancelledChecker);
|
|||
|
}
|
|||
|
|
|||
|
private static MessageBoxResult ShowProgressCore(string loadingText, Action<AlertWindow> completedHandler, Func<bool> completedChecker, Func<bool> cancelledChecker)
|
|||
|
{
|
|||
|
return RunOnUIThread(() =>
|
|||
|
{
|
|||
|
var msgWindow = AlertWindow.CreateProgressWindow(loadingText);
|
|||
|
Task.Factory.StartNew(() =>
|
|||
|
{
|
|||
|
while (true)
|
|||
|
{
|
|||
|
if (completedChecker())
|
|||
|
{
|
|||
|
Application.Current.Dispatcher.Invoke(new Action(() => completedHandler(msgWindow)));
|
|||
|
break;
|
|||
|
}
|
|||
|
else if (cancelledChecker != null && cancelledChecker())
|
|||
|
{
|
|||
|
Application.Current.Dispatcher.Invoke(new Action(() => msgWindow.Close()));
|
|||
|
break;
|
|||
|
};
|
|||
|
Thread.Sleep(500);
|
|||
|
}
|
|||
|
});
|
|||
|
if (!completedChecker())
|
|||
|
{
|
|||
|
msgWindow.ShowDialog();
|
|||
|
}
|
|||
|
return msgWindow.Result;
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|