Blazor ComponentBase 或部分类

Sea*_*ean 7 c# asp.net-core blazor

在 Blazor 中使用 ComponentBase 与 Partial Class 相比有什么大的区别或优势吗?反之亦然?我检查了微软的文档,到目前为止我还没有得到明确的答案。通过互联网搜索也没有给我带来太多信息。如果已经有类似的问题,指出我也很好。

Hen*_*man 5

使用 ComponentBase 与 Partial Class 的区别或优点

这些不是两个不同或相反的事物。您可以同时进行这两项操作。

当您将“代码隐藏类”添加到计数器页面时:

// Counter.razor.cs
partial class Counter
{
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}
Run Code Online (Sandbox Code Playgroud)

您正在使用部分类的默认规则。上面的定义完全等价于

// Counter.razor.cs
public partial class Counter : ComponentBase 
{
    ... 
}
Run Code Online (Sandbox Code Playgroud)