【问题标题】:Blazor Code-Behind: Why Is OnInitialized Not Finding Its Overridden Method?Blazor 代码隐藏:为什么 OnInitialized 没有找到其覆盖的方法?
【发布时间】: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


    【解决方案1】:

    你应该删除该行

    @inherits CreateForecast
    

    我检查了,添加 @inherits 确实会产生:

    错误 CS0115 'FetchData.BuildRenderTree(RenderTreeBuilder)': 找不到合适的方法来覆盖...

    这并不明显。

    当类尚未生成为 partial 时,在旧版本的 Blazor 中使用了@inherits

    现在他们是partial,你正试图从它自己继承一个类。

    旁注,在 CreateForecast.Razor.cs 中,您可以将定义缩短为

    //public partial class CreateForecast : ComponentBase
      partial class CreateForecast 
    

    这减少了混乱和出错的机会。

    【讨论】:

    • 我可以做到这一点,它消除了所有的 Intellisense 错误,但是当我重建并运行应用程序时,它会在创建预测时阻塞。应用程序只是说“发生了未处理的错误”。查看 Chrome 中的开发人员控制台,我得到一个空引用异常。我将添加一些 try-catch,看看是否可以缩小错误的来源。
    • 空引用是一个完全不同的错误。与此无关。
    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2013-01-16
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多