logo
为了能更好地了解您的商业使用需求,请参与 Ant Design Blazor 商业应用调查,一起建设商业应用社区,为企业系统研发赋能!

Modal对话框

Examples

第一个对话框。

expand code expand code

设置 Hender 属性可以自定义消息框的头部。默认头部组件在 ModelHeader.razor

另外,如果设置 Hender 为 null 可隐藏头部。

expand code expand code

使用 confirm() 可以快捷地弹出确认框。

expand code expand code

各种类型的信息提示,只提供一个按钮用于关闭。

expand code expand code

使用 centered 或类似 style.top 的样式来设置对话框位置。

expand code expand code

使用 Modal.destroyAll() 可以销毁弹出的确认窗。通常用于路由监听当中,处理路由前进、后退不能销毁确认对话框的问题。

expand code expand code

使用 Draggable 可以创建一个可拖拽的Modal,并且可以通过 DragInViewport 控制是否仅仅允许在视窗范围内进行拖动。

expand code expand code

使用 IConfirmService.Show 可以快捷地弹出一个内置的确认框,类似于 Windows MessageBox。

expand code expand code

将Form的submit和Modal的OK绑定

expand code expand code

修改滚动条的显示方式。

expand code expand code

通过 ModalService 创建一个 Confirm 对话框,示例中演示了用户自定义模板、自定义component。

模板代码:ConfirmTemplateDemo.razor

@inherits FeedbackComponent<string, string>

<div>
    <Text>Please input "@config"</Text>
    value: <Input @bind-Value="value" Placeholder="@config" />
</div>

@code{

    string config;

    string value;

    protected override void OnInitialized()
    {
        config = this.Options;
        base.OnInitialized();
    }


    public override async Task OnFeedbackOkAsync(ModalClosingEventArgs args)
    {
        if (FeedbackRef is ConfirmRef confirmRef)
        {
            confirmRef.Config.OkButtonProps.Loading = true;
            await confirmRef.UpdateConfigAsync();
        }
        else if (FeedbackRef is ModalRef modalRef)
        {
            modalRef.Config.ConfirmLoading = true;
            await modalRef.UpdateConfigAsync();
        }

        await Task.Delay(1000);
        // only the input's value equals the initialized value, the OK button will close the confirm dialog box
        if (value != config)
            args.Cancel = true;
        else
            // method 1(not recommended): await (FeedbackRef as ConfirmRef<string>)!.OkAsync(value);
            // method 2: await (FeedbackRef as IOkCancelRef<string>)!.OkAsync(value);
            await base.OkCancelRefWithResult!.OnOk(value);

        await base.OnFeedbackOkAsync(args);
    }

    /// <summary>
    /// If you want <b>Dispose</b> to take effect every time it is closed in Modal, which created by ModalService,
    /// set <b>ModalOptions.DestroyOnClose = true</b>
    /// </summary>
    /// <param name="disposing"></param>
    protected override void Dispose(bool disposing)
    {
        Console.WriteLine("Dispose");
        base.Dispose(disposing);
    }
}

expand code expand code

点击确定后异步关闭对话框,例如提交表单。

使用单向绑定的 Visible 控制。

expand code expand code

使用 Confirm() 可以快捷地弹出确认框。OnCancel/OnOk 异步事件 可以延迟关闭。

使用ModalClosingEventArgs.Cancel决定窗体是否关闭。返回结果: true 如果应取消事件;否则为 false。

expand code expand code

设置 okTextcancelText 以自定义按钮文字。

expand code expand code

手动更新和关闭 Modal.method 方式创建的对话框。

expand code expand code

传入 okButtonPropscancelButtonProps 可分别自定义确定按钮和取消按钮的 props。

expand code expand code

可以自定义按钮的文字,以覆盖 locale

expand code expand code

允许Modal在浏览器内最大化. Maximizable参数将会展示 Modal 最大化的按钮;DefaultMaximized 参数可以控制在 Modal 初始化的时候最大化。 MaximizableDefaultMaximized 并不相互耦合,即使 Maximizable=false, DefaultMaximized=true 也会保证 Modal 初始化的时候最大化。 注意:

  1. 如果Draggable为true,恢复大小的时候将会重置位置
  2. 当Modal最大化的时候被关闭,再次显示的时候会保持最大化的样子
expand code expand code

通过设置 Resizable=true,可以对 Modal 在水平方向进行大小调整。

expand code expand code

在Modal中使用具有下拉或弹出功能的组件。

expand code expand code

通过 ModalService 创建一个 Modal 对话框,示例中演示了自定义组件。

模板代码:ModalTemplateDemo.razor

@inherits FeedbackComponent<Form.demo.Basic.Model>

<Form Model="@_model"
      LabelCol="new ColLayoutParam { Span = 8 }"
      WrapperCol="new ColLayoutParam { Span = 16 }"
      OnFinish="OnFinish"
      OnFinishFailed="OnFinishFailed">
    <FormItem Label="Username">
        <Input @bind-Value="@context.Username" />
    </FormItem>
    <FormItem Label="Password">
        <InputPassword @bind-Value="@context.Password" />
    </FormItem>
    <FormItem WrapperCol="new ColLayoutParam{ Offset = 8, Span = 16 }">
        <Checkbox @bind-Value="context.RememberMe">Remember me</Checkbox>
    </FormItem>
    <FormItem WrapperCol="new ColLayoutParam{ Offset = 8, Span = 16 }">
        <Button Type="@ButtonType.Primary" HtmlType="submit">
            Submit
        </Button>
    </FormItem>
</Form>

<br />
<br />


@code{


    private Form.demo.Basic.Model _model;

    protected override void OnInitialized()
    {
        _model = base.Options ?? new Form.demo.Basic.Model();
        base.OnInitialized();
    }

    private void OnFinish(EditContext editContext)
    {
        Console.WriteLine($"Success:{JsonSerializer.Serialize(_model)}");
        _ = base.FeedbackRef.CloseAsync();
    }

    private void OnFinishFailed(EditContext editContext)
    {
        Console.WriteLine($"Failed:{JsonSerializer.Serialize(_model)}");
    }
}

expand code expand code

Modal API#

属性 描述 类型 默认值
AfterClose 属性指定一个将在模式关闭时调用的函数 Func<Task> --
BodyStyle 属性模态主体元素的主体样式。比如高度,填充等 String --
CancelText 属性Cancel 按钮的 Text 或 RenderFragment,它将覆盖 ModalLocale OneOf<String, RenderFragment?? --
Centered 属性居中 Boolean --
Closable 属性关闭 (x) 按钮是否在模态对话框的右上角可见 Boolean true
Draggable 属性模态对话框是否被拖动 Boolean --
DragInViewport 属性仅在视口内拖放 Boolean true
CloseIcon 属性关闭图标RenderFragment,默认是一个“X” RenderFragment close-outline
ConfirmLoading 属性是否为确定按钮应用加载视觉效果 Boolean --
DestroyOnClose 属性onClose 时是否卸载子组件 Boolean false
Header 属性标题内容 RenderFragment --
Footer 属性页脚内容,不需要默认按钮时设置为Footer=null OneOf<String, RenderFragment?? --
GetContainer 属性获取或设置模态父 DOM,默认为 null:指定 document.body ElementReference? --
Keyboard 属性是否支持按esc关闭 Boolean true
Mask 属性是否显示掩码 Boolean true
MaskClosable 属性单击遮罩(模态之外的区域)时是否关闭模态对话框 Boolean true
MaskStyle 属性模态遮罩元素的样式 String --
OkText 属性确定按钮的 RenderFragment 文本,它将覆盖 ModalLocale OneOf<String, RenderFragment?? null
OkType 属性确定按钮的按钮类型 String ButtonType.Primary
Title 属性模态对话框的标题 String --
TitleTemplate 属性模态对话框的标题。优先于标题。 RenderFragment --
Visible 属性模态对话框是否可见 Boolean --
VisibleChanged 属性指定模式对话框可见或不可见时的函数调用 EventCallback<Boolean> --
Width 属性模态对话框的宽度,默认值为520 OneOf<String, Double> 520
WrapClassName 属性模态对话框容器的类名 String --
ZIndex 属性模态的 z-index Int32 1000
OnCancel 属性指定当用户单击蒙版、右上角的关闭按钮或取消按钮时将调用的函数 EventCallback<MouseEventArgs> --
OnOk 属性指定当用户单击“确定”按钮时将调用的函数 EventCallback<MouseEventArgs> --
OkButtonProps 属性确定按钮道具 ButtonProps --
CancelButtonProps 属性取消按钮道具 ButtonProps --
ChildContent 属性子内容 RenderFragment --
Rtl 是RTL Boolean --
Locale 属性模态语言环境 ModalLocale LocaleProvider.CurrentLocacle.Modal
MaxBodyHeight 属性最大模态内容主体高度 String --
Maximizable 属性显示模态最大化按钮 Boolean false
MaximizeBtnIcon 属性模态框正常状态下最大化按钮的图标 RenderFragment fullscreen-outline
RestoreBtnIcon 属性模态最大化时最大化按钮的图标 RenderFragment fullscreen-exit-outline
DefaultMaximized 属性在组件初始化时最大化 Modal,它会忽略 Maximizable 值。 Boolean false
Resizable 属性可调整大小(仅限水平方向) Boolean --
ForceRender 属性是否强制在打开之前渲染模态 dom。 Boolean --
Id 属性组件 HTML 的 ID String Uniquely Generated ID
Class 属性为 DOM 元素指定一个或多个类名。 String --
Style 属性指定 DOM 元素的内联样式。 String --
RefBack 属性一个 ForwardRef 实例。您可以使用 AntDesign.ForwardRef.Current 获取对内部 DOM 的引用。 ForwardRef --

ModalService API#

方法签名 返回类型 描述
Confirm(ConfirmOptions props) ConfirmRef 创建并打开一个 OK-Cancel Confirm 对话框
Info(ConfirmOptions options) ConfirmRef 创建并打开带有信息图标的 OK-Cancel Confirm 对话框
Success(ConfirmOptions options) ConfirmRef 创建并打开带有成功图标的 OK-Cancel Confirm 对话框
Error(ConfirmOptions options) ConfirmRef 创建并打开带有错误图标的 OK-Cancel Confirm 对话框
Warning(ConfirmOptions options) ConfirmRef 创建并打开带有警告图标的 OK-Cancel Confirm 对话框
ConfirmAsync(ConfirmOptions props) Task<Boolean> 创建并打开一个 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮
InfoAsync(ConfirmOptions options) Task<Boolean> 创建并打开带有信息图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮
SuccessAsync(ConfirmOptions options) Task<Boolean> 创建并打开带有成功图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮
ErrorAsync(ConfirmOptions options) Task<Boolean> 创建并打开带有错误图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮
WarningAsync(ConfirmOptions options) Task<Boolean> 创建并打开带有警告图标的 OK-Cancel Confirm 对话框,并返回一个 bool 值,指示是否单击了 OK 按钮
Update(ConfirmRef confirmRef) Task 更新确认哪个 Visible=true
UpdateConfirmAsync(ConfirmRef confirmRef) Task 更新确认哪个 Visible=true
Destroy(ConfirmRef confirmRef) Task 关闭确认对话框
DestroyConfirmAsync(ConfirmRef confirmRef) Task 关闭确认对话框
DestroyAll() Task 关闭所有确认对话框
DestroyAllConfirmAsync() Task 关闭所有确认对话框
CreateAsync(ConfirmOptions config) Task<ConfirmRef> 创建并开启一个OK-Cancel Confirm异步
CreateConfirmAsync(ConfirmOptions config) Task<ConfirmRef> 创建并开启一个OK-Cancel Confirm异步
CreateAsync(ConfirmOptions config, TComponentOptions componentOptions) Task<ConfirmRef<TResult>> 创建并打开模板确认对话框
CreateConfirmAsync(ConfirmOptions config, TComponentOptions componentOptions) Task<ConfirmRef<TResult>> 创建并打开模板确认对话框
CreateModal(ModalOptions options) ModalRef 创建并打开模态
CreateModal(ModalOptions options) ModalRef<TResult> 创建并打开模态
CreateModal(ModalOptions options, TComponentOptions componentOptions) ModalRef 使用模板创建并打开模态
CreateModal(ModalOptions options, TComponentOptions componentOptions) ModalRef<TResult> 创建并打开 Modal with template 组件
CreateModalAsync(ModalOptions options) Task<ModalRef> 创建并打开模态
CreateModalAsync(ModalOptions options) Task<ModalRef<TResult>> 创建并打开模态
CreateModalAsync(ModalOptions options, TComponentOptions componentOptions) Task<ModalRef> 使用模板创建并打开模态
CreateModalAsync(ModalOptions options, TComponentOptions componentOptions) Task<ModalRef<TResult>> 使用模板创建并打开模态

ConfirmService API#

方法签名 返回类型 描述
Show(OneOf<String, RenderFragment> content, OneOf<String, RenderFragment> title, ConfirmButtons confirmButtons, ConfirmIcon confirmIcon, ConfirmButtonOptions options, ConfirmAutoFocusButton? autoFocusButton) Task<ConfirmResult> 显示确认对话框,如 Windows 的 MessageBox
Show(OneOf<String, RenderFragment> content, OneOf<String, RenderFragment> title, ConfirmButtons confirmButtons, ConfirmIcon confirmIcon) Task<ConfirmResult> 显示确认对话框,如 Windows 的 MessageBox

ConfirmOptions API#

属性 描述 类型 默认值
Locale 确认语言环境 ConfirmLocale --
ClassName “.ant-modal”元素的类名 String --
Visible Boolean --
Content 儿童内容 OneOf<String, RenderFragment> --
Icon 确认左上角图标 RenderFragment --
Style .ant-modal 元素的样式 String --
AutoFocusButton ConfirmAutoFocusButton --
OkType 为最左边的按钮设置 OK 按钮类型:OK 或 Yes 按钮 String --
OkText 为最左边的按钮设置 OK 按钮内容:OK 或 Yes 按钮,它将覆盖 ConfirmLocale OneOf<String, RenderFragment?? --
CancelText 为左侧第二个按钮设置取消按钮内容:取消或否按钮,它将覆盖 ConfirmLocale OneOf<String, RenderFragment?? --
OnCancel 对于 OK-Cancel 确认对话框,取消按钮点击回调。仅在ModalService方式创建的Confirm中触发 Func<ModalClosingEventArgs, Task> --
OnOk 对于 OK-Cancel Confirm 对话框,OK 按钮点击回调。仅在ModalService方式创建的Confirm中触发 Func<ModalClosingEventArgs, Task> --
OkButtonProps OK-Cancel Confirm 对话框的 OK 按钮属性。它相当于 Button1Props。 ButtonProps --
CancelButtonProps OK-Cancel 确认对话框的取消按钮道具。它相当于 Button2Props。 ButtonProps --
Button1Props LTR 布局中最左边的按钮 ButtonProps --
Button2Props 左边第二个按钮是LTR布局 ButtonProps --
Button3Props LTR 布局中最右边的按钮 ButtonProps --
OkCancel 显示 OK-Cancel Confirm 对话框的取消按钮 Boolean --
PrefixCls 类名前缀 String --
CancelText 模态默认页脚取消文本 OneOf<String, RenderFragment> --
Centered 是否中置显示 Boolean --
GetContainer 获取或设置模态父 DOM ElementReference? --
Keyboard 是否支持按esc关闭 Boolean --
Mask 是否显示掩码 Boolean --
MaskClosable 单击遮罩(模态之外的区域)时是否关闭模态对话框 Boolean --
MaskStyle 对话框的掩码元素的样式 String --
OkText 确定按钮的文本 OneOf<String, RenderFragment> --
Title 模态对话框的字符串标题 String --
TitleTemplate RenderFragment 模态对话框的标题 RenderFragment --
Width 模态对话框的宽度 OneOf<String, Double> --
ZIndex 模态的 z-index Int32 --
Rtl 是RTL Boolean --
Message全局提示 Notification通知提醒框
文档已更新,请点击 此处 更新。
A new version of this app is available. Click here to update.