using System.Windows.Controls; using WPFluent.Controls; namespace WPFluent; /// /// Represents a contract with the service that creates . /// /// /// <ContentPresenter x:Name="RootContentDialogPresenter" Grid.Row="0" /> 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" } ); /// public class ContentDialogService { private ContentPresenter? _dialogHost; /// /// 提供对 的直接访问。 /// /// 引用当前选定的 显示 的内容。 public ContentPresenter? GetDialogHost() { return _dialogHost; } /// /// Sets the /// /// /// inside of which the dialogue will be placed. The new will /// replace the current . /// public void SetDialogHost(ContentPresenter contentPresenter) { _dialogHost = contentPresenter; } /// /// Asynchronously shows the specified dialog. /// /// The dialog to be displayed. /// A cancellation token that can be used to cancel the operation. /// A task that represents the asynchronous operation. The task result contains the dialog result. public Task 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); } }