热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Blazor确认复选框组件

目录介绍使用代码Startup.csApp.razor_Host.cshtmlConfirmationCheckBox.razorConfirmationCheckB

目录

介绍

使用代码

Startup.cs

App.razor

_Host.cshtml

ConfirmationCheckBox.razor

ConfirmationCheckBox.razor.cs

如何使用?

ConfirmCheckBox.razor




  • GitHub下载示例代码

介绍

在我使用Blazor.net的一项作业中&#xff0c;很少有表单将输入控件作为复选框。根据要求&#xff0c;在更改事件之后在EditForm<\input type&#61;"checkbox" />中使用InputCheckBox组件处理复选框值是很困难的。下面列出了一些限制使用默认组件的逻辑&#xff1a;


  1. 点击确认模态弹窗后恢复复选框的值
  2. 以最少的代码句柄来处理上述功能

为了实现上述功能&#xff0c;需要创建一个自定义组件来单独处理复选框并避免代码重复。该组件使用第三方包&#xff0c;即Blazored Modal&#xff0c;它是免费提供的Nuget包管理器。他们网站上提供的详细配置可以参考&#xff0c;以便在应用程序中集成。这可以根据应用程序中的用法进行替换。

下面是相同的代码以及如何在不同场景中使用它的解释。


使用代码

代码就像一个简单的插件&#xff0c;只需配置组件并在应用程序中使用它。下面是相同的代码。


Startup.cs

将服务引用添加到blazored modalBlazored模态相关配置。

services.AddBlazoredModal();



App.razor

CascadingBlazoredModal包裹路由器标签。Blazored模态相关配置。


.....


_Host.cshtml


提供对Blazored Modal脚本和样式表的参考。Blazored模态相关配置。





ConfirmationCheckBox.razor

根据以下代码在项目的共享UI文件夹中创建一个razor组件。例如&#xff0c;BlazorApp/Pages/Component





ConfirmationCheckBox.razor.cs

razor组件所在的文件夹中创建与上述相同的razor.cs类。以下代码充当创建的Razor页面的后端代码。

#region System References
using System;
using System.Threading.Tasks;
#endregion
#region BlazorApp Reference
using BlazorApp.Pages.Modal;
using Blazored.Modal;
using Blazored.Modal.Services;
#endregion
#region Microsoft References
using Microsoft.AspNetCore.Components;
#endregion
namespace BlazorApp.Pages.Components
{public partial class ConfirmationCheckBox{#region Parameter// Parameter for div that wraps the input checkbox[Parameter] public string Class {get;set;}// Parameter for Checkbox name[Parameter] public string Name {get;set;}// Parameter to defined message on popup when checkbox is checked[Parameter] public string PopupMessageForTrue {get;set;}// Parameter to defined message on popup when checkbox is unchecked[Parameter] public string PopupMessageForFalse {get;set;}// Parameter to defined event call back on Popup button action[Parameter] public EventCallback OnPopupClicked {get;set;}// Parameter to make the checkbox readonly[Parameter] public bool ReadOnly {get;set;}// Parameter to define id of the checkbox[Parameter] public string Id {get;set;}// Parameter to bind the value of checkbox[Parameter] public bool? Value{get&#61;>_value;set{if(_value&#61;&#61;value) return;_value&#61;value;ValueChanged.InvokeAsync(value);}}// Parameter to handle two way binding[Parameter] public EventCallback ValueChanged {get;set;}#endregion#region Inject[Inject] IModalService Modal {get;set;}#endregionbool? _value;string Message;#region Protected Methodprotected override void OnInitialized(){Class&#61;string.IsNullOrEmpty(Class)?"checkbox":Class; }#endregion#region Private Method///

/// Method to change the Check box change event/// /// ChangeEventArgs/// private async Task OnChecked(ChangeEventArgs args){bool argsValue&#61;Convert.ToBoolean(args.Value);Message&#61; !argsValue ? PopupMessageForTrue : PopupMessageForFalse;Message &#61; string.IsNullOrEmpty(Message) ? "Are you sure ?" : Message;var options&#61; new ModalOptions() {DisableBackgroundCancel&#61;true,HideCloseButton&#61;true};ModalParameters parameter &#61; new ModalParameters();parameter.Add("Body", Message);var formModal &#61; Modal.Show("Confirm", parameter,options);var result&#61; await formModal.Result;if(!result.Cancelled){if(Convert.ToBoolean(result.Data))await ValueChanged.InvokeAsync(argsValue);elseawait ValueChanged.InvokeAsync(!argsValue);await OnPopupClicked.InvokeAsync(Convert.ToBoolean(result.Data));}StateHasChanged();}#endregion}
}


下面是每个变量和函数的描述&#xff1a;


组件属性

描述

Value

要传递的模型属性

Name

名称与复选框组件对齐

ValueChanged

在值更改时设置模型属性的事件回调。有助于双向绑定

Id

要设置的组件的id属性

Class

包装输入复选框的div类属性

ReadOnly

要设置的禁用属性&#xff0c;即如果true&#xff0c;其他启用则禁用

PopupMessageForTrue

如果选中复选框&#xff0c;将在模态弹出窗口中显示消息

PopupMessageForFalse

如果未选中复选框&#xff0c;将在模态弹出窗口中显示消息

 


方法

描述

OnInitializedAsync

在组件初始化时执行的方法

OnChecked

在输入字段的复选框检查中调用的方法


如何使用&#xff1f;

下面是一组示例代码&#xff0c;展示了如何使用双向绑定来使用上述组件。


ConfirmCheckBox.razor

下面是显示如何使用能够组件的示例代码。

&#64;page "/ConfirmationCheckBox"

PopupClicked(value))"PopupMessageForTrue&#61;"Are You sure ? It is checked."PopupMessageForFalse&#61;"Are You sure ? It is unchecked." />

CheckBoxValue : &#64;CheckBoxValue

Popup Value : &#64;PopupValue



&#64;code{ private bool? CheckBoxValue{get;set;}private bool? PopupValue {get;set;}/// /// Method to be invoked on popup action by ConfirmationCheckBox component/// /// bool/// private void PopupClicked(bool value){PopupValue&#61;value;}
}


示例代码可在GitHub 上获得

https://www.codeproject.com/Tips/5304619/Blazor-Confirmation-Checkbox-Component


推荐阅读
author-avatar
女女的家_747
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有