#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=>_value;set{if(_value==value) return;_value=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=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=Convert.ToBoolean(args.Value);Message= !argsValue ? PopupMessageForTrue : PopupMessageForFalse;Message = string.IsNullOrEmpty(Message) ? "Are you sure ?" : Message;var options= new ModalOptions() {DisableBackgroundCancel=true,HideCloseButton=true};ModalParameters parameter = new ModalParameters();parameter.Add("Body", Message);var formModal = Modal.Show("Confirm", parameter,options);var result= 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} }
下面是每个变量和函数的描述:
组件属性
描述
Value
要传递的模型属性
Name
名称与复选框组件对齐
ValueChanged
在值更改时设置模型属性的事件回调。有助于双向绑定
Id
要设置的组件的id属性
Class
包装输入复选框的div类属性
ReadOnly
要设置的禁用属性,即如果true,其他启用则禁用
PopupMessageForTrue
如果选中复选框,将在模态弹出窗口中显示消息
PopupMessageForFalse
如果未选中复选框,将在模态弹出窗口中显示消息
方法
描述
OnInitializedAsync
在组件初始化时执行的方法
OnChecked
在输入字段的复选框检查中调用的方法
如何使用?
下面是一组示例代码,展示了如何使用双向绑定来使用上述组件。
ConfirmCheckBox.razor
下面是显示如何使用能够组件的示例代码。
@page "/ConfirmationCheckBox"
PopupClicked(value))"PopupMessageForTrue="Are You sure ? It is checked."PopupMessageForFalse="Are You sure ? It is unchecked." />
CheckBoxValue : @CheckBoxValue
Popup Value : @PopupValue
@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=value;} }