最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Blazor實現(xiàn)高級表單功能

2022-07-17 13:29 作者:限量版范兒  | 我要投稿

思路

  1. 添加Form組件類,提供Validate,OnSubmit等

  2. 添加Field組件基類,提供Id,Label,Value等

  3. 添加Field子組件Text、Password等表單字段組件

  4. 添加FormContext類,存儲表單及字段數(shù)據(jù)

  5. 使用級聯(lián)值組件傳遞FormContext實例(關鍵)

Form組件

public class FormContext { ? ?public Dictionary<string, Field> Fields { get; } } public class Form : BaseComponent { ? ?[Parameter] public RenderFragment ChildContent { get; set; } ? ?public FormContext Context { get; } ? ?public bool Validate() { return false; } ? ? ? ?protected override void BuildRenderTree(RenderTreeBuilder builder) { ? ? ? ?builder.Div(attr => { ? ? ? ? ? ?attr.Class(Style); ? ? ? ? ? ?builder.Component<CascadingValue<FormContext>>(attr => { ? ? ? ? ? ? ? ?attr.Add(nameof(CascadingValue<FormContext>.Value), Context); ? ? ? ? ? ? ? ?attr.Add(nameof(CascadingValue<FormContext>.ChildContent), ChildContent); ? ? ? ? ? ?}); ? ? ? ?}); ? ?} }

Field組件

public class Field : BaseComponent { ? ?[Parameter] public string Id { get; set; } ? ?[Parameter] public string Label { get; set; } ? ?[CascadingParameter] ? ?protected FormContext Context { get; set; } ? ?protected override void OnInitialized() { ? ? ? ?base.OnInitialized(); ? ? ? ?Context.Fields[Id] = this; ? ?} ? ?protected override void BuildRenderTree(RenderTreeBuilder builder) { ? ?} ? ?protected virtual void BuidChildContent(RenderTreeBuilder builder) {} } public class Text : Field { ? ?[Parameter] public string Icon { get; set; } ? ?[Parameter] public string Placeholder { get; set; } ? ?protected override void BuidChildContent(RenderTreeBuilder builder) { ? ?} } public class Password : Field { ? ?[Parameter] public string Icon { get; set; } ? ?[Parameter] public string Placeholder { get; set; } ? ?protected override void BuidChildContent(RenderTreeBuilder builder) { ? ?} }

Form示例

<Form @ref="form"> ? ?<Text Id="UserName" Icon="fa fa-user-o" Placeholder="用戶名" /> ? ?<Password Id="Password" Icon="fa fa-lock" Placeholder="密碼" /> ? ?<Button Text="登 錄" OnClick="OnLogin" /> </Form> @code { ? ?private Form form; ? ?private void OnLogin() { ? ? ? ?if (!form.Validate()) ? ? ? ? ? ?return; ? ?} }

來源:https://www.dianjilingqu.com/203138.html

Blazor實現(xiàn)高級表單功能的評論 (共 條)

分享到微博請遵守國家法律
石阡县| 连城县| 乌海市| 建水县| 香格里拉县| 贵阳市| 钟祥市| 安达市| 汉沽区| 勐海县| 潞城市| 城固县| 乌恰县| 都匀市| 齐河县| 柘城县| 平邑县| 蓬莱市| 临汾市| 蒲城县| 上饶县| 兴仁县| 塔河县| 洛隆县| 江西省| 南阳市| 扶沟县| 宁化县| 武定县| 乌拉特后旗| 万山特区| 宿松县| 神木县| 永春县| 呈贡县| 锡林郭勒盟| 宣威市| 永福县| 合肥市| 益阳市| 阳高县|