-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1943 from ButchersBoy/master
add mechanism for easy access of a dialog from a view model
- Loading branch information
Showing
9 changed files
with
342 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
|
||
namespace MahApps.Metro.Controls.Dialogs | ||
{ | ||
public class DialogCoordinator : IDialogCoordinator | ||
{ | ||
/// <summary> | ||
/// Gets the default instance if the dialog coordinator, which can be injected into a view model. | ||
/// </summary> | ||
public static readonly DialogCoordinator Instance = new DialogCoordinator(); | ||
|
||
public Task<string> ShowInputAsync(object context, string title, string message, MetroDialogSettings metroDialogSettings = null) | ||
{ | ||
var metroWindow = GetMetroWindow(context); | ||
|
||
return metroWindow.ShowInputAsync(title, message, metroDialogSettings); | ||
} | ||
|
||
public Task<LoginDialogData> ShowLoginAsync(object context, string title, string message, MessageDialogStyle style = MessageDialogStyle.Affirmative, LoginDialogSettings settings = null) | ||
{ | ||
var metroWindow = GetMetroWindow(context); | ||
|
||
return metroWindow.ShowLoginAsync(title, message, settings); | ||
} | ||
|
||
public Task<MessageDialogResult> ShowMessageAsync(object context, string title, string message, MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null) | ||
{ | ||
var metroWindow = GetMetroWindow(context); | ||
|
||
return metroWindow.ShowMessageAsync(title, message, style, settings); | ||
} | ||
|
||
public Task<ProgressDialogController> ShowProgressAsync(object context, string title, string message, | ||
bool isCancelable = false, MetroDialogSettings settings = null) | ||
{ | ||
var metroWindow = GetMetroWindow(context); | ||
|
||
return metroWindow.ShowProgressAsync(title, message, isCancelable, settings); | ||
} | ||
|
||
public Task ShowMetroDialogAsync(object context, BaseMetroDialog dialog, | ||
MetroDialogSettings settings = null) | ||
{ | ||
var metroWindow = GetMetroWindow(context); | ||
|
||
return metroWindow.ShowMetroDialogAsync(dialog, settings); | ||
} | ||
|
||
public Task HideMetroDialogAsync(object context, BaseMetroDialog dialog, MetroDialogSettings settings = null) | ||
{ | ||
var metroWindow = GetMetroWindow(context); | ||
|
||
return metroWindow.HideMetroDialogAsync(dialog, settings); | ||
} | ||
|
||
public Task<TDialog> GetCurrentDialogAsync<TDialog>(object context) where TDialog : BaseMetroDialog | ||
{ | ||
var metroWindow = GetMetroWindow(context); | ||
|
||
return metroWindow.GetCurrentDialogAsync<TDialog>(); | ||
} | ||
|
||
private static MetroWindow GetMetroWindow(object context) | ||
{ | ||
if (context == null) throw new ArgumentNullException("context"); | ||
if (!DialogParticipation.IsRegistered(context)) | ||
throw new InvalidOperationException( | ||
"Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext."); | ||
|
||
var association = DialogParticipation.GetAssociation(context); | ||
var metroWindow = Window.GetWindow(association) as MetroWindow; | ||
|
||
if (metroWindow == null) | ||
throw new InvalidOperationException("Control is not inside a MetroWindow."); | ||
return metroWindow; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Windows; | ||
|
||
namespace MahApps.Metro.Controls.Dialogs | ||
{ | ||
public static class DialogParticipation | ||
{ | ||
private static readonly IDictionary<object, DependencyObject> ContextRegistrationIndex = new Dictionary<object, DependencyObject>(); | ||
|
||
public static readonly DependencyProperty RegisterProperty = DependencyProperty.RegisterAttached( | ||
"Register", typeof(object), typeof(DialogParticipation), new PropertyMetadata(default(object), RegisterPropertyChangedCallback)); | ||
|
||
private static void RegisterPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) | ||
{ | ||
if (dependencyPropertyChangedEventArgs.OldValue != null) | ||
ContextRegistrationIndex.Remove(dependencyPropertyChangedEventArgs.OldValue); | ||
|
||
if (dependencyPropertyChangedEventArgs.NewValue != null) | ||
ContextRegistrationIndex[dependencyPropertyChangedEventArgs.NewValue] = dependencyObject; | ||
} | ||
|
||
public static void SetRegister(DependencyObject element, object context) | ||
{ | ||
element.SetValue(RegisterProperty, context); | ||
} | ||
|
||
public static object GetRegister(DependencyObject element) | ||
{ | ||
return element.GetValue(RegisterProperty); | ||
} | ||
|
||
internal static bool IsRegistered(object context) | ||
{ | ||
if (context == null) throw new ArgumentNullException("context"); | ||
|
||
return ContextRegistrationIndex.ContainsKey(context); | ||
} | ||
|
||
internal static DependencyObject GetAssociation(object context) | ||
{ | ||
if (context == null) throw new ArgumentNullException("context"); | ||
|
||
return ContextRegistrationIndex[context]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace MahApps.Metro.Controls.Dialogs | ||
{ | ||
/// <summary> | ||
/// Use the dialog coordinator to help you interfact with dialogs from a view model. | ||
/// </summary> | ||
public interface IDialogCoordinator | ||
{ | ||
/// <summary> | ||
/// Shows the input dialog. | ||
/// </summary> | ||
/// <param name="context">Typically this should be the view model, which you register in XAML using <see cref="DialogParticipation.SetRegister"/>.</param> | ||
/// <param name="title">The title of the MessageDialog.</param> | ||
/// <param name="message">The message contained within the MessageDialog.</param> | ||
/// <param name="settings">Optional settings that override the global metro dialog settings.</param> | ||
/// <returns>The text that was entered or null (Nothing in Visual Basic) if the user cancelled the operation.</returns> | ||
Task<string> ShowInputAsync(object context, string title, string message, MetroDialogSettings settings = null); | ||
|
||
/// <summary> | ||
/// Creates a LoginDialog inside of the current window. | ||
/// </summary> | ||
/// <param name="context">Typically this should be the view model, which you register in XAML using <see cref="DialogParticipation.SetRegister"/>.</param> | ||
/// <param name="title">The title of the LoginDialog.</param> | ||
/// <param name="message">The message contained within the LoginDialog.</param> | ||
/// <param name="style"></param> | ||
/// <param name="settings">Optional settings that override the global metro dialog settings.</param> | ||
/// <returns>The text that was entered or null (Nothing in Visual Basic) if the user cancelled the operation.</returns> | ||
Task<LoginDialogData> ShowLoginAsync(object context, string title, string message, MessageDialogStyle style = MessageDialogStyle.Affirmative, LoginDialogSettings settings = null); | ||
|
||
/// <summary> | ||
/// Creates a MessageDialog inside of the current window. | ||
/// </summary> | ||
/// <param name="context">Typically this should be the view model, which you register in XAML using <see cref="DialogParticipation.SetRegister"/>.</param> | ||
/// <param name="title">The title of the MessageDialog.</param> | ||
/// <param name="message">The message contained within the MessageDialog.</param> | ||
/// <param name="style">The type of buttons to use.</param> | ||
/// <param name="settings">Optional settings that override the global metro dialog settings.</param> | ||
/// <returns>A task promising the result of which button was pressed.</returns> | ||
Task<MessageDialogResult> ShowMessageAsync(object context, string title, string message, MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null); | ||
|
||
/// <summary> | ||
/// Creates a ProgressDialog inside of the current window. | ||
/// </summary> | ||
/// <param name="context">Typically this should be the view model, which you register in XAML using <see cref="DialogParticipation.SetRegister"/>.</param> | ||
/// <param name="title">The title of the ProgressDialog.</param> | ||
/// <param name="message">The message within the ProgressDialog.</param> | ||
/// <param name="isCancelable">Determines if the cancel button is visible.</param> | ||
/// <param name="settings">Optional Settings that override the global metro dialog settings.</param> | ||
/// <returns>A task promising the instance of ProgressDialogController for this operation.</returns> | ||
Task<ProgressDialogController> ShowProgressAsync(object context, string title, string message, | ||
bool isCancelable = false, MetroDialogSettings settings = null); | ||
|
||
/// <summary> | ||
/// Adds a Metro Dialog instance to the specified window and makes it visible asynchronously. | ||
/// <para>You have to close the resulting dialog yourself with <see cref="HideMetroDialogAsync"/>.</para> | ||
/// </summary> | ||
/// <param name="context">Typically this should be the view model, which you register in XAML using <see cref="DialogParticipation.SetRegister"/>.</param> | ||
/// <param name="dialog">The dialog instance itself.</param> | ||
/// <param name="settings">An optional pre-defined settings instance.</param> | ||
/// <returns>A task representing the operation.</returns> | ||
/// <exception cref="InvalidOperationException">The <paramref name="dialog"/> is already visible in the window.</exception> | ||
Task ShowMetroDialogAsync(object context, BaseMetroDialog dialog, | ||
MetroDialogSettings settings = null); | ||
|
||
/// <summary> | ||
/// Hides a visible Metro Dialog instance. | ||
/// </summary> | ||
/// <param name="context">Typically this should be the view model, which you register in XAML using <see cref="DialogParticipation.SetRegister"/>.</param> | ||
/// <param name="dialog">The dialog instance to hide.</param> | ||
/// <param name="settings">An optional pre-defined settings instance.</param> | ||
/// <returns>A task representing the operation.</returns> | ||
/// <exception cref="InvalidOperationException"> | ||
/// The <paramref name="dialog"/> is not visible in the window. | ||
/// This happens if <see cref="ShowMetroDialogAsync"/> hasn't been called before. | ||
/// </exception> | ||
Task HideMetroDialogAsync(object context, BaseMetroDialog dialog, MetroDialogSettings settings = null); | ||
|
||
/// <summary> | ||
/// Gets the current shown dialog. | ||
/// </summary> | ||
/// <param name="context">Typically this should be the view model, which you register in XAML using <see cref="DialogParticipation.SetRegister"/>.</param> | ||
Task<TDialog> GetCurrentDialogAsync<TDialog>(object context) where TDialog : BaseMetroDialog; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.