属性值更改时如何通知Blazor Framework [英] How Blazor Framework get notifed when the property value gets changed

查看:188
本文介绍了属性值更改时如何通知Blazor Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有如下所示的HTML代码时.

When we have a HTML code like below.

<h1>@Title</h1>

和下面的C#

public string Title { get; set; }

认为我已经执行了如下代码

Think I have executed a code like below

Title = "New title updated in the runtime";

然后我们看到该视图已经有了新的价值.

Then we see that the view has chenged with the new value.

渲染器如何知道属性值已更改. 是否有实现INotifyProperyChange之类的东西. HTML如何知道现在是时候更新内容了.

How the renderer knows that the property value has changes. Is there anything like INotifyProperyChange is implemented. How the HTML knows that this is the time to update the content.

我只想知道Blazor框架的内部结构如何.

I just want to know how the internals of the Blazor Framework are architectured.

推荐答案

当用户进行交互(例如,单击按钮,更改输入文本)时,Blazor会自动检查绑定属性的更改.在这种情况下,Blazor的JavaScript代码将触发C#(= Webassembly)中的更改检测.因此,如果要在用户交互后刷新UI,则无需执行任何操作.

Blazor automatically checks for changes of bound properties when a user interaction happened (e.g. button click, text in an input changed). In that case, Blazor's JavaScript code triggers change detection in C# (=Webassembly). Therefore, you do not need to do anything if you want to refresh the UI after a user interaction.

但是,在某些情况下,尽管没有发生用户交互(例如计时器),但您仍要刷新UI.在这种情况下,您必须调用StateHasChanged.我在 learn-blazor.com 上创建了更详细的示例.

However, there are cases in which you want to refresh the UI although no user interaction happened (e.g. timer). In that case, you have to call StateHasChanged. I have created a more detailed example on learn-blazor.com.

这篇关于属性值更改时如何通知Blazor Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆