我有一个服务器端 Blazor 应用程序。在 Blazor 下编写剃须刀页面时有两种选择:将设计代码和 C# 代码放在一个“.razor”文件中,并将设计和代码文件分开为“.razor”和“.razor.cs”文件。问题是 Visual Studio 不完全支持剃须刀页面中的智能感知,但您始终可以将代码放在单独的文件中并使用完整的智能感知支持。这是一个包含代码块的最小示例剃须刀页面:
@page "/minimalsample"
<h3>MinimalSample</h3>
<MatChip Raised="true" @onclick="@OpenUp" @ref="SampleMenuButton" Label="Actions" LeadingIcon="cloud_download"></MatChip>
<MatMenu @ref="SampleMenu">
<MatList SingleSelection="false" TwoLine="false">
<MatListItem OnClick="@Format_Clicked" Style="height: auto">
Format C: Drive
</MatListItem>
<MatListItem OnClick="@Shred_Clicked" Style="height: auto">
Shred files
</MatListItem>
</MatList>
</MatMenu>
<br />
@ActionText
@code {
private BaseMatMenu SampleMenu;
private BaseMatChip SampleMenuButton;
private string ActionText;
private void OpenUp()
{
SampleMenu.OpenAsync(SampleMenuButton.Ref);
}
private void Format_Clicked()
{
ActionText = "Formatting C: Drive...";
}
private void Shred_Clicked()
{
ActionText = "Shredding user files...";
}
}
此代码工作正常。当我单击按钮时它会打开一个菜单,并且菜单项也可以正常工作。我决定将代码和设计文件分开,以便能够使用 Visual Studio (2019) 的智能感知功能,现在我收到警告说:
警告 CS0649 字段“MinimalSample.SampleMenu”从未分配给,并且始终具有其默认值 null
“.razor”页面如下:
@page "/minimalsample"
@namespace Pages
<h3>MinimalSample</h3>
<MatChip Raised="true" @onclick="@OpenUp" @ref="SampleMenuButton" Label="Actions" LeadingIcon="cloud_download"></MatChip>
<MatMenu @ref="SampleMenu">
<MatList SingleSelection="false" TwoLine="false">
<MatListItem OnClick="@Format_Clicked" Style="height: auto">
Format C: Drive
</MatListItem>
<MatListItem OnClick="@Shred_Clicked" Style="height: auto">
Shred files
</MatListItem>
</MatList>
</MatMenu>
<br />
@ActionText
代码文件“.razor.cs”是:
using MatBlazor;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Pages
{
public partial class MinimalSample : ComponentBase
{
private BaseMatMenu SampleMenu;
private BaseMatChip SampleMenuButton;
private string ActionText;
private void OpenUp()
{
SampleMenu.OpenAsync(SampleMenuButton.Ref);
}
private void Format_Clicked()
{
ActionText = "Formatting C: Drive...";
}
private void Shred_Clicked()
{
ActionText = "Shredding user files...";
}
}
}
我不得不更改按钮和菜单引用以具有一些默认值(只是为了让编译器满意!)。
private BaseMatMenu SampleMenu = new BaseMatMenu();
private BaseMatChip SampleMenuButton = new BaseMatChip();
private string ActionText = "";
我知道这不是必需的,并且在没有新操作员的情况下也可以正常工作。然而编译器仍然抱怨变量“ActionText”被赋值,但它的值从未被使用过。
我想以适当的方式摆脱警告(将代码移动到 .razor 页面不是这里的选项)。我能做些什么来摆脱这些警告?有没有可能这是一个编译器错误?
更新:
@NikProtsman 提供了一个可行的解决方案。我刚刚删除了部分修饰符并将“.razor.cs”中的类定义的名称从
public partial class MinimalSample : ComponentBase
至
public class MinimalSampleBase : ComponentBase
我还将私有修饰符更改为受保护
...
...
public class MinimalSampleBase : ComponentBase
{
protected BaseMatMenu SampleMenu;
protected BaseMatChip SampleMenuButton;
protected string ActionText;
protected void OpenUp()
{
SampleMenu.OpenAsync(SampleMenuButton.Ref);
}
protected void Format_Clicked()
{
ActionText = "Formatting C: Drive...";
}
protected void Shred_Clicked()
{
ActionText = "Shredding user files...";
}
}
...
...
后来我像这样更改了“.razor”页面
@page "/minimalsample"
@namespace Pages
// added this line
@inherits MinimalSampleBase
...
...
现在我没有收到警告,我的代码按预期运行。