简体   繁体   English

如何通过调用 Blazor 中的另一个组件方法来更改按钮文本

[英]How to change Button Text by calling another component method in Blazor

I have a custom Button component like below.我有一个自定义按钮组件,如下所示。

<button class="btn btn-outline-primary" @onclick="OnClick"> @ButtonText</button>

@code {

    [Parameter]
    public string ButtonText { get; set; } = "Edit";

    public virtual void OnClick()
    {
        ButtonText = ButtonText == "Edit" ? "Editing..." : "Edit";
    }
}

When I click button every thing works as excepted and Button text is changing.当我单击按钮时,一切都正常工作,并且按钮文本正在发生变化。 But when i click on another button i want to change the text of the button.To do this I call the button's onclick method.但是当我单击另一个按钮时,我想更改按钮的文本。为此,我调用按钮的 onclick 方法。 But when I click the button, the text does not change even though the method is called.但是当我单击按钮时,即使调用了该方法,文本也不会改变。 Here is my page.这是我的页面。

@page "/test"

<EditButton @ref="EditButton"></EditButton>

<button class="btn-primary" @onclick="ChangeButtonText">Change Button Text</button>

@code {

    EditButton EditButton;

    void ChangeButtonText()
    {
        EditButton.OnClick();
    }
}

It's partly due to your architectural design: The ButtonComponent should not be managing the editing-status, it should just display it.这部分是由于您的架构设计:ButtonComponent 不应该管理编辑状态,它应该只是显示它。

@page "/test"

<EditButton IsEditing="isEditing" Click="EditClick"></EditButton>

<button class="btn-primary" @onclick="ChangeButtonText">Change Button Text</button>

@code {

    //EditButton EditButton;
    bool isEditing = false;

    void ChangeButtonText()
    {
        isEditing = false;  // or !isEditing
    }

    void EditClick()
    {
       isEditing = ! isEditing;   
    }

}

Your code does not work simply because it lacks a call to the StateHasChanged method:您的代码不能正常工作,因为它缺少对StateHasChanged方法的调用:

EditButton.razor编辑按钮.razor

<button class="btn btn-outline-primary" @onclick="OnClick"> @ButtonText</button>

@code {

    [Parameter]
    public string ButtonText { get; set; } = "Edit";

    public void OnClick()
    {
        ButtonText = ButtonText == "Edit" ? "Editing..." : "Edit";

        InvokeAsync(() => StateHasChanged());
    }
} 

Note That I added a call to the StateHasChanged method to re-render the component after its state has changed.请注意,我添加了对 StateHasChanged 方法的调用,以便在组件的 state 发生更改后重新渲染组件。

InvokeAsync(() => StateHasChanged());

Index.razor索引.razor

@page "/"

<EditButton @ref="EditButton"></EditButton>

<button class="btn-primary" @onclick="ChangeButtonText">Change Button Text</button>

@code {

    EditButton EditButton;

    void ChangeButtonText()
    {
        EditButton.OnClick();
    }
}

To sum up: What you do is fine, but you must call the StateHasChaged method manually.总结一下:你做的很好,但是你必须手动调用 StateHasChaged 方法。 It is not called automatically by the framework.它不是由框架自动调用的。 Note that it is not necessary to add a call to the StateHasChaged method from event handler that handle UI event, such as the 'click' event.请注意,没有必要从处理 UI 事件(例如“单击”事件)的事件处理程序中添加对 StateHasChaged 方法的调用。 But in your case it is necessary, as the EditButton.OnClick event handler is not a UI event handler.但在您的情况下,这是必要的,因为 EditButton.OnClick 事件处理程序不是 UI 事件处理程序。 That was the only issue with your code.这是您的代码的only问题。

IMPORTANT: You should not modify or change the state of a Component parameter property.重要提示:您不应修改或更改组件参数属性的 state。 This should be avoided:应该避免这种情况:

[Parameter]
public string ButtonText { get; set; } = "Edit";

As well as this:还有这个:

ButtonText = ButtonText == "Edit" ? "Editing..." : "Edit";

Component parameter properties should be automatic properties:组件参数属性应该是自动属性:

[Parameter]
public string ButtonText { get; set; } 

And they must never be modified outside of the component.并且它们绝不能在组件之外进行修改。 Read this Read this .阅读此阅读此 Instead you should define local variable or properties that are assigned from the component parameter properties' values, on which you can apply whatever manipulations you want.相反,您应该定义从组件参数属性的值分配的局部变量或属性,您可以在其上应用您想要的任何操作。

Not adhering to that rule may result in unfavorable side effects in large components, only Steve Sanderson can discern.不遵守该规则可能会导致大型组件产生不利的副作用,只有 Steve Sanderson 可以辨别。

What about this simple solution.这个简单的解决方案怎么样。 EditButton remains the same. EditButton保持不变。

@page "/"

<EditButton ButtonText="@this.editButtonText" ></EditButton>

<button class="btn btn-primary ml-3" @onclick="ChangeButtonText">Change Button Text</button>

@code {
    string editButtonText = "Edit";

    void ChangeButtonText()
    {
        editButtonText = editButtonText.Equals("Edit")
        ? "Editing"
        : "Edit";
    }
}

You don't need to hook up callbacks and references.您不需要连接回调和引用。 When you change the value of ButtonText on your component and trigger a Blazor Component event handler - ChangeButtonText - on the parent, the Renderer detects the change to ButtonText and informs EditButton by calling SetParametersSetAsync .当您更改组件上ButtonText的值并触发父组件上的 Blazor 组件事件处理程序 - ChangeButtonText - 时,渲染器会检测到对ButtonText的更改并通过调用SetParametersSetAsync通知EditButton This precipitates a render event on EditButton and the button text gets updated.这会在EditButton上触发一个渲染事件,并且按钮文本会得到更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 blazor 中的另一个组件启动方法? - How to initiate an method from another component in blazor? 如何从另一个 Blazor(“子”)组件调用一个 Blazor(父)组件中的方法? - How do I call a method in one Blazor (parent) component from another Blazor (“child”) component? 如何使用方法更改按钮的文本? - How to change the text of a button with a method? Blazor - 一个组件应该对另一个组件的方法做出反应 - Blazor - one component should react to a method of another component 从@ref 组件引用调用方法时,Blazor 绑定不起作用 - Blazor binding is not working when calling a method from @ref component reference 如何在 Blazor 中实现按钮下拉组件 - How to Implement Button dropdown component in Blazor 如何通过单击按钮动态生成 Blazor 组件? - How to generate a Blazor component dynamically on a click of a button? 如何使用 SyncFusion Blazor 调用某个类中另一个类的方法来刷新组件? - How to call a method of another class in some class using SyncFusion Blazor to refresh a component? 如何使用 Blazor 服务器将文本框中输入的值从模式窗口传递到另一个组件? - How to pass the value entered in a text box from a modal window to another component using Blazor server? C#从另一种方法更改button.text - c# change button.text from another method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM