Blazor UI:通知服务
IUiNotificationService用于在用户界面上显示Toast风格的通知。
快速示例
只需在页面或组件中注入IUiNotificationService,然后调用Success方法即可显示成功消息。
namespace MyProject.Blazor.Pages
{
public partial class Index
{
private readonly IUiNotificationService _uiNotificationService;
public Index(IUiNotificationService uiNotificationService)
{
_uiNotificationService = uiNotificationService;
}
public async Task DeleteAsync()
{
await _uiNotificationService.Success(
"产品'Acme Atom Re-Arranger'已成功删除。"
);
}
}
}
如果您的页面或组件继承自AbpComponentBase类,则可以使用Notify属性访问预注入的IUiNotificationService。
namespace MyProject.Blazor.Pages
{
public partial class Index : AbpComponentBase
{
public async Task DeleteAsync()
{
await Notify.Success(
"产品'Acme Atom Re-Arranger'已成功删除。"
);
}
}
}
通常您可以在
.razor文件中使用@inherits AbpComponentBase来继承AbpComponentBase,而不是在代码隐藏文件中继承。
通知类型
系统预定义了四种通知类型:
Info(...)- 信息通知Success(...)- 成功通知Warn(...)- 警告通知Error(...)- 错误通知
以上所有方法都接受以下参数:
message:要显示的消息内容(字符串)title:可选的标题(字符串)options:用于配置通知选项的可选操作(Action)
配置
单次通知配置
如果您希望为单个通知自定义选项,可以轻松修改默认通知配置。如下所示,向options参数提供一个操作:
await UiNotificationService.Success(
"产品'Acme Atom Re-Arranger'已成功删除。",
options: (options) =>
{
options.OkButtonText =
LocalizableString.Create<MyProjectNameResource>("CustomOK");
});
可用选项
以下是所有可用选项列表:
OkButtonText:自定义确定按钮文本OkButtonIcon:自定义确定按钮图标
全局配置
您还可以配置全局通知选项,以便在统一位置进行控制。在模块的ConfigureServices方法中配置UiNotificationOptions选项类:
Configure<UiNotificationOptions>(options =>
{
options.OkButtonText = LocalizableString.Create<MyProjectNameResource>("CustomOK");
});
此处可用的选项与上述相同。
单次通知配置会覆盖默认值。
抠丁客



