首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blazor中的单独代码文件提供虚假的警告消息。

Blazor中的单独代码文件提供虚假的警告消息。
EN

Stack Overflow用户
提问于 2020-04-25 14:50:44
回答 2查看 816关注 0票数 3

我有一个服务器端的Blazor应用程序。在使用Blazor编写剃须刀页面时,有两种选择:将代码和C#代码放在一个".razor“文件中,并将设计和代码文件分隔为".razor”和".razor.cs“文件。问题是Visual并不完全支持剃须刀页面中的intellisense,但是您始终可以将代码放到单独的文件中,并使用完全的intellisense支持。下面是一个包含代码块的最小样本剃须刀页面:

代码语言:javascript
复制
@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 (2019)的intellisense特性,我决定将代码和设计文件分开,现在我收到警告说:

警告CS0649字段“MinimalSample.SampleMenu”从未分配给,并且始终具有默认值null。

".razor“页面如下:

代码语言:javascript
复制
@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“是:

代码语言:javascript
复制
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...";
        }

    }
}

我不得不更改按钮和菜单引用,以获得一些默认值(只是为了使编译器高兴!)

代码语言:javascript
复制
    private BaseMatMenu SampleMenu = new BaseMatMenu();
    private BaseMatChip SampleMenuButton = new BaseMatChip();
    private string ActionText = "";

我知道这是不必要的,没有新的操作符,它也能工作。然而编译器仍然抱怨变量"ActionText“被赋值,但它的值从未被使用过。

我想以正确的方式消除警告(将代码移动到.razor页面并不是一个选项)。我怎样才能摆脱这些警告呢?这有可能是编译器的错误吗?

更新:

@NikProtsman提供了一个可行的解决方案。我刚刚删除了部分修饰符,并将".razor.cs“中的类定义的名称从

代码语言:javascript
复制
public partial class MinimalSample : ComponentBase

代码语言:javascript
复制
public class MinimalSampleBase : ComponentBase

我还将私有修饰符更改为受保护的

代码语言:javascript
复制
...
...
    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“页面

代码语言:javascript
复制
@page "/minimalsample"
@namespace Pages
// added this line
@inherits MinimalSampleBase
...
...

现在我没有收到警告,我的代码正在按预期运行。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-27 15:50:05

这些文档显示了使用部分Class方法,但是我总是比较幸运地使用继承模型。对于.razor.cs文件,可以尝试将类称为基:public class MinimalSampleBase : ComponentBase,然后在.razor文件中添加@inherits MinimalSampleBase。您需要在标记页上使用的所有private字段和方法现在应该改为protected,但是支持字段和支持方法仍然应该标记为protected。基类现在变成了一个“视图模型”,并且仍然得到了对Intellisense和重构的充分支持,并且您得到了代码背后方法的所有好处,并且希望在您的例子中,它将使那些编译器警告消失。

我还发现了部分类方法,在需要实现接口的任何地方都会出现问题,比如设置服务的事件订阅,以及使用IDisposable取消对解压的订阅。然而,继承模型工作得很好。

请试试看,报告一下,我想会有帮助的,但我很好奇能不能确定一下。

票数 3
EN

Stack Overflow用户

发布于 2020-04-27 13:27:55

看起来我们需要等待.NET 5:“这看起来是一个问题,我们如何在构建前步骤中生成Blazor文件的骨架,因为构建前步骤删除了@functions / @code之外的所有Razor内容。这在技术上是由源生成器修复的,但这些都在.NET 5的切线上。”

https://github.com/dotnet/aspnetcore/issues/20137

编辑:我确认这个问题在.NET 5中已经解决了。它在没有继承/保护属性的情况下工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61427520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档