47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
|
|
using System.Windows.Controls;
|
|
|
|
using WPFluent.Controls;
|
|
|
|
namespace WPFluent;
|
|
|
|
/// <summary>
|
|
/// Represents a contract with the service that creates <see cref="ContentDialog"/>.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <code lang="xml"> <ContentPresenter x:Name="RootContentDialogPresenter" Grid.Row="0" /></code> <code
|
|
/// lang="csharp"> IContentDialogService contentDialogService = new ContentDialogService();
|
|
/// contentDialogService.SetContentPresenter(RootContentDialogPresenter); await _contentDialogService.ShowAsync( new
|
|
/// ContentDialog(){ Title = "The cake?", Content = "IS A LIE!", PrimaryButtonText = "Save", SecondaryButtonText =
|
|
/// "Don't Save", CloseButtonText = "Cancel" } );</code>
|
|
/// </example>
|
|
public class ContentDialogService : IContentDialogService
|
|
{
|
|
private ContentPresenter? _dialogHost;
|
|
|
|
/// <inheritdoc/>
|
|
public ContentPresenter? GetDialogHost() { return _dialogHost; }
|
|
|
|
|
|
/// <inheritdoc/>
|
|
public void SetDialogHost(ContentPresenter contentPresenter) { _dialogHost = contentPresenter; }
|
|
|
|
/// <inheritdoc/>
|
|
public Task<ContentDialogResult> ShowAsync(ContentDialog dialog, CancellationToken cancellationToken)
|
|
{
|
|
if (_dialogHost == null)
|
|
{
|
|
throw new InvalidOperationException("The DialogHost was never set.");
|
|
}
|
|
|
|
if (dialog.DialogHost != null && _dialogHost != dialog.DialogHost)
|
|
{
|
|
throw new InvalidOperationException("The DialogHost is not the same as the one that was previously set.");
|
|
}
|
|
|
|
dialog.DialogHost = _dialogHost;
|
|
|
|
return dialog.ShowAsync(cancellationToken);
|
|
}
|
|
}
|