【问题标题】:Dealing with a basic Blazor form - Missing directive or assembly reference?处理基本的 Blazor 表单 - 缺少指令或程序集参考?
【发布时间】:2021-08-23 01:22:45
【问题描述】:

我已经尝试创建我的第一个 Blazor 表单,它似乎已经完成了。我遇到的问题是:创建私有命名空间时 - 我认为它在这里被称为命名空间:

private _ExampleForm _exampleForm = new();

Visual Studio 说“找不到命名空间 _ExampleForm(是 您缺少指令或程序集参考?)

完整的代码在这里,我几乎是从官方Blazor documentation复制过来的

@page "/opportunity"
@using Microsoft.AspNet.Identity.EntityFramework
@using Microsoft.AspNet.Identity
@using System.Security.Cryptography.X509Certificates
@using Microsoft.Extensions.Logging    
   
<EditForm Model="@_exampleForm" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />    
    <InputText id="name" @bind-Value="_exampleForm.Name" />    
    <button type="submit">Submit</button>
</EditForm>
    
@code {    
    private _ExampleForm _exampleForm = new(); //PROBLEM LINE   
    private void HandleValidSubmit()
    {
    // do something
    }
  }

我在 Visual Studio 中使用带有自动 C# 版本检测功能的 .NET CORE 3.0。

【问题讨论】:

  • 嗨@MattHodson,在文档中是ExampleModel 而不是_ExampleForm 。你配置_ExampleForm 模型了吗?

标签: asp.net-core blazor blazor-server-side


【解决方案1】:

请务必在项目根目录中配置_ExampleForm.cs:

namespace BlazorApp1
{
    public class _ExampleForm
    {
        public string Name { get; set; }
    }
}

否则,您需要指定命名空间:

例如:如果模型存在于 Models 文件夹中

namespace BlazorApp1.Models
{
    public class _ExampleForm
    {
        public string Name { get; set; }
    }
}

剃须刀文件:

@using BlazorApp1.Models;   //be sure add namespace

@code {
    private _ExampleForm exampleModel = new();

}

【讨论】:

  • 这需要 C# 9 吗?尽管拥有 .NET CORE 3,Visual Studio 还是自动将我置于 C# 8 上,这会导致 new() 错误,因为它是 C# 9 的功能。
  • 如果您使用的是 .net core 3,则需要在 .csproj 文件中将语言版本明确设置为 9。见docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
猜你喜欢
  • 2014-01-09
  • 2023-04-01
  • 2014-02-16
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
相关资源
最近更新 更多