【发布时间】:2021-10-13 04:59:42
【问题描述】:
我在 Visual Studio 2022 中使用 .NET 6.0.100-preview.6.21355.2。我从 WebAssembly 模板开始,然后跟随 this YouTube tutorial 在 WebAssembly 下通过 HTTP API 实现 SQL。该教程有一个带有its code的GitHub页面。
所有这些都运行良好,然后我决定将代码从 Razor 页面移到代码隐藏页面中。第一页(counter.razor / counter.razor.cs)工作正常,但第二页给我带来了问题。具体来说,我在 Intellisense for OnInitialized 中收到 no suitable method found to override 错误。
OnInitialized 有一个空签名,就像它在 CreateForecast.razor 和 this Microsoft document 中一样。那么为什么会出错呢?奇怪的是,如果我将代码返回到基本 Razor 页面,我会收到相同的错误。还有一些其他 Intellisense 错误,但我怀疑包括这些错误会一次问多个问题:-)。
我还尝试查找 OnInitialized 签名以确保那里没有发生奇怪的事情。它在 ComponentBase 中列为受保护的 void 虚拟,没有参数。因此,我希望后代类中没有参数的受保护的覆盖 void 方法会匹配。
这里是 CreateForecast.Razor:
@page "/createforecast"
@using CART.Shared
@using CART.Client
@inherits CreateForecast
<h3>Create Forecast</h3>
<div class="row">
<EditForm Model="@Forecast" OnValidSubmit="InsertForecast">
<div class="form-group">
<label>Date: </label>
<InputDate @bind-Value="Forecast.Date" />
</div>
<div class="form-group">
<label>Temperature (C): </label>
<InputNumber @bind-Value="Forecast.TemperatureC" />
</div>
<div class="form-group">
<label>Summary: </label>
<InputText @bind-Value="Forecast.Summary" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" value="Create forecast" />
<NavLink href="fetchData" class="btn btn-info">Back to forecasts</NavLink>
</div>
</EditForm>
</div>
(CART 是应用程序的名称。)
这里是 CreateForecast.Razor.cs:
using CART.Shared;
using Microsoft.AspNetCore.Components;
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Json;
namespace CART.Client.Pages
{
public partial class CreateForecast : ComponentBase
{
public WeatherForecast Forecast { get; set; }
public HttpClient httpClient;
public NavigationManager navigationManager;
protected override void OnInitialized()
{
Forecast.Date = DateTime.Today;
}
public async Task InsertForecast()
{
await httpClient.PostAsJsonAsync("/api/weatherforecast", Forecast);
navigationManager.NavigateTo("/fetchdata");
}
}
}
【问题讨论】:
标签: c# razor blazor blazor-webassembly asp.net-blazor