Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组件中的调用方法_C#_.net Core_Blazor - Fatal编程技术网

C# 组件中的调用方法

C# 组件中的调用方法,c#,.net-core,blazor,C#,.net Core,Blazor,我有这个“警报”组件: @if (Show) { <div class="alert @Class" role="alert"> @Text </div> } @functions { [Parameter] private bool Show { get; set; } = false; [Parameter] private string Text { get; set; } = String.Emp

我有这个“警报”组件:

@if (Show)
{
    <div class="alert @Class" role="alert">
        @Text
    </div>
}

@functions
{
    [Parameter]
    private bool Show { get; set; } = false;

    [Parameter]
    private string Text { get; set; } = String.Empty;

    [Parameter]
    private string Class { get; set; } = String.Empty; //Success, Warning etc.
}
@if(显示)
{
@正文
}
@功能
{
[参数]
私有bool Show{get;set;}=false;
[参数]
私有字符串文本{get;set;}=string.Empty;
[参数]
私有字符串类{get;set;}=string.Empty;//成功、警告等。
}
然而,当我在我的页面上调用这个组件时,我仍然需要创建至少两个变量——shourror和ErrorText——来处理这个警报的状态,因为这个警报几乎存在于所有页面上,所以我的代码仍然非常混乱

我的问题是:是否可以通过调用子组件中的ShowMessage方法来消除代码混乱?

例如:

页面

@page "/my-page"
@inject HttpClient Http


<!-- A lot of HTML code here -->

<Alert/>

<!-- A lot of HTML code here -->


@functions {

    protected override async Task OnInitAsync()
    {
        var response = await Http.PostJsonAsync<Response>("/api/sessions/create", null);
        if (response.StatusCode == HttpStatusCode.OK)
        {

        }
        else
        {
            myAlertComponent.ShowSuccessMessage(response.Message);
        }
    }
}
@page”/my page
@注入HttpClient Http
@功能{
受保护的重写异步任务OnInitAsync()
{
var response=wait Http.PostJsonAsync(“/api/sessions/create”,null);
if(response.StatusCode==HttpStatusCode.OK)
{
}
其他的
{
myAlertComponent.ShowSuccessMessage(response.Message);
}
}
}
警报组件

@if (Show)
{
    <div class="alert @Class" role="alert">
    @Text
    </div>
}

@functions
{
    [Parameter]
    private bool Show { get; set; } = false;

    [Parameter]
    private string Text { get; set; } = String.Empty;

    [Parameter]
    private string Class { get; set; } = String.Empty; //Success, Warning, Danger

    public void HideAlerts()
    {
    Show = false;
    }

    public void ShowSuccessMessage(string message)
    {
    Show = true;
    Text = message;
    Class = "success":
    }

    public void ShowErrorMessage(string message)
    {
    Show = true;
    Text = message;
    Class = "danger":
    }
}
@if(显示)
{
@正文
}
@功能
{
[参数]
私有bool Show{get;set;}=false;
[参数]
私有字符串文本{get;set;}=string.Empty;
[参数]
私有字符串类{get;set;}=string.Empty;//成功、警告、危险
公共空间藏身处()
{
Show=false;
}
公共无效ShowSuccessMessage(字符串消息)
{
Show=true;
文本=消息;
Class=“成功”:
}
公共消息(字符串消息)
{
Show=true;
文本=消息;
Class=“危险”:
}
}

这就是.NET(本例中为Blazor)中组件之间通信的执行方式

页面组件 定义封装具有单个参数的方法的操作委托

     public event Action<string> DisplayAlert;

       protected override async Task OnInitAsync()
       {
            var response = await Http.PostJsonAsync<Response>     ("/api/sessions/create", null);

            if (response.StatusCode == HttpStatusCode.OK)
            {
               // Success should be here, I believe
               NotifyStateChanged(response.Message);
            }
            else
            {

            }
        }

    //Invoke any methods added to the event delegate:



private void NotifyStateChanged(string message) =>  DisplayAlert?.Invoke(message);
公共事件动作显示预警;
受保护的重写异步任务OnInitAsync()
{
var response=wait Http.PostJsonAsync(“/api/sessions/create”,null);
if(response.StatusCode==HttpStatusCode.OK)
{
//我相信成功就在这里
NotifyStateChanged(response.Message);
}
其他的
{
}
}
//调用添加到事件委托的任何方法:
私有void NotifyStateChanged(字符串消息)=>DisplayAlert?.Invoke(消息);
这是: 将对ShowSuccessMessage方法的引用分配给父组件中定义的DisplayAlert委托的实例

<Alert DisplayAlert = "@( str => ShowSuccessMessage(str))/>

<!-- A lot of HTML code here -->

 -------------------------------------------------------------------------------    

"Alert" component
----------------
Define a method whose signature corresponds to that defined by the delegate 




 private static void ShowSuccessMessage(string message)
       {
           Show = true;
           Text = message;
           Class = "success":    
       }