Blazor Webassembly - 为什么在 TimerOnElapsed 上调用函数?
Blazor Webassembly - Why does function get called on TimerOnElapsed?
我正在玩 Blazor Webassembly 模板,特别是天气预报页面。
我只是添加了一个计时器,并有一个增加变量的函数,该函数显示在页面上。我没有得到的是,从未专门调用 addText 函数,但是变量随着计时器的流逝而不断增加。所以我不明白这里发生了什么。
谁能赐教一下? html 区域内的所有函数是否都在 StateHasChanged() 或其他东西上被调用?如果我将 html 中的行更改为“@((MarkupString)sometext.ToString())”,它不会递增。这是代码:
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
<br />
@((MarkupString)addText())
}
@code {
private WeatherForecast[] forecasts;
private int sometext = 0;
Timer timer;
protected override async Task OnInitializedAsync()
{
timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += TimerOnElapsed;
timer.Start();
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
StateHasChanged();
}
private string addText()
{
return sometext++.ToString();
}
}
StateHaseChanged
正在强制渲染。渲染器正在调用函数...标记区域被编译为在渲染器上调用的委托。您的函数只是一个子程序调用。您正在计算渲染调用。
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
<br />
@sometext
}
@code {
private WeatherForecast[] forecasts;
private int sometext = 0;
Timer timer;
protected override async Task OnInitializedAsync()
{
timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += TimerOnElapsed;
timer.Start();
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
sometext++;
StateHasChanged(); // <- probably not needed not sure
}
}
我正在玩 Blazor Webassembly 模板,特别是天气预报页面。
我只是添加了一个计时器,并有一个增加变量的函数,该函数显示在页面上。我没有得到的是,从未专门调用 addText 函数,但是变量随着计时器的流逝而不断增加。所以我不明白这里发生了什么。
谁能赐教一下? html 区域内的所有函数是否都在 StateHasChanged() 或其他东西上被调用?如果我将 html 中的行更改为“@((MarkupString)sometext.ToString())”,它不会递增。这是代码:
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
<br />
@((MarkupString)addText())
}
@code {
private WeatherForecast[] forecasts;
private int sometext = 0;
Timer timer;
protected override async Task OnInitializedAsync()
{
timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += TimerOnElapsed;
timer.Start();
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
StateHasChanged();
}
private string addText()
{
return sometext++.ToString();
}
}
StateHaseChanged
正在强制渲染。渲染器正在调用函数...标记区域被编译为在渲染器上调用的委托。您的函数只是一个子程序调用。您正在计算渲染调用。
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
<br />
@sometext
}
@code {
private WeatherForecast[] forecasts;
private int sometext = 0;
Timer timer;
protected override async Task OnInitializedAsync()
{
timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += TimerOnElapsed;
timer.Start();
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
}
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
{
sometext++;
StateHasChanged(); // <- probably not needed not sure
}
}