Bootstrap Blazor 实战 Dialog 对话框组件快速入门

这篇博客介绍了如何在应用中注入并使用DialogService和SwalService进行弹窗操作。通过示例展示了创建说明对话框、组件对话框、编辑对话框的方法,并提到了SweetAlert弹窗组件的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方文档

1. 注入服务 DialogService
@inject DialogService DialogService

或者后置代码

[Inject]
[NotNull]
private DialogService? DialogService { get; set; }
2. 弹出说明
await DialogService.Show(new DialogOption()
{
    Title = "运行结果",
    IsScrolling = true,
    IsCentered = true,
    BodyTemplate = new RenderFragment(builder =>
    {
        builder.OpenElement(1, "p");
        builder.AddContent(2, "结果1结果2结果3结果4");
        builder.CloseElement();
    })
});

image

[特别介绍] SweetAlert 弹窗组件

SweetAlert

[Inject]
[NotNull]
private SwalService? SwalService { get; set; }

var op = new SwalOption()
{
    Title = Localizer["SwalOptionTitle"],
    Content = Localizer["SwalOptionContent"]
};
var ret = await SwalService.ShowModal(op);

image

3. 弹出组件
DialogService.Show(new DialogOption()
{
    Title = "Built-in Counter component",
    Component = BootstrapDynamicComponent.CreateComponent<Counter>()
});

image

4. 弹出编辑框
    string CurrentDirectory = "";
    void LoadFiles() { }
    public class NewFolders
    {

        [DisplayName("文件夹名称")]
        public string NewFolderName { get; set; }

    }


    private async Task ShowNewFolderDialog()
    {
        var newFolders = new NewFolders();
        var option = new EditDialogOption<NewFolders>()
        {
            Title = "新文件夹名称",
            Model = newFolders,

            OnEditAsync = context =>
            {
                string path = Path.Combine(CurrentDirectory, newFolders.NewFolderName);

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                    LoadFiles();
                }

                return Task.FromResult(true);
            }
        };

        await DialogService.ShowEditDialog(option);
    }

image

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Densen2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值