DialogService
The DialogService is a service that can be used to show dialogs. It can be injected into a page or component and is used to show different type of dialogs.
For a component to be useable by the DialogService, it needs to implement IDialogContentComponent<T>
.
The DialogService is automatically registered in the DI container with the .AddFluentUIComponents()
method call.
Dialog provider
Dialogs are rendered by the <FluentDialogProvider />
. This component needs to be added to the main layout of your application/site.
You typically do this in the MainLayout.razor
file at the end of the <main>
section like this:
<main>
<nav>
<!-- -->
</nav>
<div class="content">
<article id="article">
@Body
</article>
</div>
<FluentDialogProvider />
</main>
IMPORTANT!!
For the
<FluentDialogProvider/>
to work properly, it needs interactivity! If you are using "per page" interactivity or Server Side Rendering (ASP.NET Core 8 or above), make sure to add a@rendermode
to either the provider itself or the component the provider is placed in.
See the following pages for examples on how to use the DialogService with the different types of dialogs.
Exchange data between dialog and calling component
There are two ways to exchange data between the dialog and the component which calls the dialog. The first is by capturing the returned IDialogReference
from one of the DialogService.Show...Async
methods and then use that
reference to get the dialog's result (of type DialogResult
). The second is by using an EventCalback
parameter as part of the
DialogParameters
.
Using async/await with an IDialogReference
You can use the DialogService.Show...Async()
methods to show a dialog. When the type of dialog supports data exchange with the
callingcomponent, these methods return an IDialogReference
which can be used to get the dialog's result. The
DialogResult
contains the data that was passed to and altered in the dialog. It also contains a Cancelled
property
which indicates if the dialog was cancelled or not.
A typical implementation could look something like this:
DialogParameters<SimplePerson> parameters = new()
{
Title = $"Hello {simplePerson.Firstname}",
PrimaryAction = "Yes",
PrimaryActionEnabled = false,
SecondaryAction = "No",
Width = "500px",
Height = "500px",
Content = simplePerson,
TrapFocus = _trapFocus,
Modal = _modal,
};
IDialogReference dialog = await DialogService.ShowDialogAsync<SimpleDialog, SimplePerson>(parameters);
DialogResult? result = await dialog.Result;
if (result.Data is not null)
{
SimplePerson? simplePerson = result.Data as SimplePerson;
Console.WriteLine($"Dialog closed by {simplePerson?.Firstname} {simplePerson?.Lastname} ({simplePerson?.Age}) - Canceled: {result.Cancelled}");
}
else
{
Console.WriteLine($"[DialogService] Dialog closed - Canceled: {result.Cancelled}");
}
Using an EventCallback
You can exchange data between the component that opened the dialog and the dialog component by using the DialogParameters.Content
parameter to specify the type of data and providing a callback function to the DialogParameters.OnDialogResult
method.
Any changes you make to the data in the Dialog component will be reflected in the Data object in the calling component when the dialog is closed.
You can use the CreateDialogCallback
method in the Dialogservice
to create the callback function.
Typically a call would look like this:
OnDialogResult = DialogService.CreateDialogCallback(this, HandleIt),
where this
represents the dialog calling component and HandleIt
is the method in the calling component that will be called
when the dialog is closed. It receives the DialogResult
as a parameter.
An typical implemtation could look something like this:
private async Task HandleIt(DialogResult result)
{
if (result.Cancelled)
{
//Handle the cancellation
return;
}
if (result.Data is not null)
{
//Handle the data
}
//Handle closing the dialog
await Task.Run(() => ...);
}
DialogService Class
Methods
Name | Parameters | Type | Description |
---|---|---|---|
CloseAsync | DialogReference dialog | Task | |
CloseAsync | DialogReference dialog DialogResult result | Task | |
CreateDialogCallback | Object receiver Func<DialogResult, Task> callback | EventCallback<DialogResult> | Convenience method to create a Microsoft.AspNetCore.Components.EventCallback for a dialog result.You can also call EventCallback.Factory.Create directly. |
ShowConfirmation | Object receiver Func<DialogResult, Task> callback string message string primaryText string secondaryText string title | void | Shows a confirmation message box. Has a callback function which returns boolean (true=PrimaryAction clicked, false=SecondaryAction clicked). |
ShowConfirmationAsync | Object receiver Func<DialogResult, Task> callback string message string primaryText string secondaryText string title | Task<IDialogReference> | Shows a confirmation message box. Has no callback function (true=PrimaryAction clicked, false=SecondaryAction clicked). |
ShowConfirmationAsync | string message string primaryText string secondaryText string title | Task<IDialogReference> | Shows a confirmation message box. Has no callback function (true=PrimaryAction clicked, false=SecondaryAction clicked). |
ShowDialog<TData> | Type dialogComponent TData content DialogParameters parameters | void | |
ShowDialog<TDialog, TData> | DialogParameters<TData> parameters | void | |
ShowDialogAsync | RenderFragment renderFragment DialogParameters dialogParameters | Task<IDialogReference> | |
ShowDialogAsync<TData> | Type dialogComponent TData data DialogParameters parameters | Task<IDialogReference> | |
ShowDialogAsync<TDialog, TData> | TData data DialogParameters parameters | Task<IDialogReference> | |
ShowDialogAsync<TDialog, TData> | DialogParameters<TData> parameters | Task<IDialogReference> | |
ShowDialogAsync<TDialog> | Object data DialogParameters parameters | Task<IDialogReference> | |
ShowDialogAsync<TDialog> | DialogParameters parameters | Task<IDialogReference> | |
ShowError | string message string title string primaryAction | void | Shows an error message box. Does not have a callback function. |
ShowErrorAsync | string message string title string primaryAction | Task<IDialogReference> | Shows an error message box. Does not have a callback function. |
ShowInfo | string message string title string primaryAction | void | Shows an information message box. Does not have a callback function. |
ShowInfoAsync | string message string title string primaryAction | Task<IDialogReference> | Shows an information message box. Does not have a callback function. |
ShowMessageBox | DialogParameters<MessageBoxContent> parameters | void | Shows a custom message box. Has a callback function which returns boolean (true=PrimaryAction clicked, false=SecondaryAction clicked). |
ShowMessageBoxAsync | DialogParameters<MessageBoxContent> parameters | Task<IDialogReference> | Shows a custom message box. Has a callback function which returns boolean (true=PrimaryAction clicked, false=SecondaryAction clicked). |
ShowPanel<TData> | Type dialogComponent DialogParameters<TData> parameters | void | |
ShowPanel<TDialog, TData> | DialogParameters<TData> parameters | void | |
ShowPanelAsync<TData> | Type dialogComponent TData data DialogParameters parameters | Task<IDialogReference> | |
ShowPanelAsync<TData> | Type dialogComponent DialogParameters<TData> parameters | Task<IDialogReference> | |
ShowPanelAsync<TDialog, TData> | DialogParameters<TData> parameters | Task<IDialogReference> | |
ShowPanelAsync<TDialog> | Object data DialogParameters parameters | Task<IDialogReference> | |
ShowPanelAsync<TDialog> | DialogParameters parameters | Task<IDialogReference> | |
ShowSplashScreen | Object receiver Func<DialogResult, Task> callback DialogParameters<SplashScreenContent> parameters | void | Shows a splash screen of the given type with the given parameters. |
ShowSplashScreen | Type component Object receiver Func<DialogResult, Task> callback DialogParameters<SplashScreenContent> parameters | void | Shows a splash screen of the given type with the given parameters. |
ShowSplashScreen<T> | Object receiver Func<DialogResult, Task> callback DialogParameters<SplashScreenContent> parameters | void | Shows a custom splash screen dialog with the given parameters. |
ShowSplashScreenAsync | Object receiver Func<DialogResult, Task> callback DialogParameters<SplashScreenContent> parameters | Task<IDialogReference> | Shows a splash screen of the given type with the given parameters. |
ShowSplashScreenAsync | DialogParameters<SplashScreenContent> parameters | Task<IDialogReference> | Shows a splash screen of the given type with the given parameters. |
ShowSplashScreenAsync | Type component Object receiver Func<DialogResult, Task> callback DialogParameters<SplashScreenContent> parameters | Task<IDialogReference> | Shows a splash screen of the given type with the given parameters. |
ShowSplashScreenAsync | Type component DialogParameters<SplashScreenContent> parameters | Task<IDialogReference> | Shows a splash screen of the given type with the given parameters. |
ShowSplashScreenAsync<T> | Object receiver Func<DialogResult, Task> callback DialogParameters<SplashScreenContent> parameters | Task<IDialogReference> | Shows a custom splash screen dialog with the given parameters. |
ShowSplashScreenAsync<T> | DialogParameters<SplashScreenContent> parameters | Task<IDialogReference> | Shows a custom splash screen dialog with the given parameters. |
ShowSuccess | string message string title string primaryAction | void | Shows a success message box. Does not have a callback function. |
ShowSuccessAsync | string message string title string primaryAction | Task<IDialogReference> | Shows a success message box. Does not have a callback function. |
ShowWarning | string message string title string primaryAction | void | Shows a warning message box. Does not have a callback function. |
ShowWarningAsync | string message string title string primaryAction | Task<IDialogReference> | Shows a warning message box. Does not have a callback function. |
UpdateDialog<TData> | string id DialogParameters<TData> parameters | void | |
UpdateDialogAsync<TData> | string id DialogParameters<TData> parameters | Task<IDialogReference> |
FluentDialogProvider Class
Methods
Name | Parameters | Type | Description |
---|---|---|---|
DismissAll | void |