【问题标题】:Inherited base class that inherits ComponentBase in partial class gives error在部分类中继承 ComponentBase 的继承基类给出错误
【发布时间】:2021-11-10 10:04:31
【问题描述】:

我正在使用 something.razor 文件和Something.razor.cs 文件后面的代码。我的部分类继承自 SomethingBase,SomethingBase 本身继承自 ComponentBase。

然而这会产生错误

CS0115 .buildrendertree(rendertreebuilder)': 在部分类中找不到合适的方法来覆盖 CS0263 的部分声明不能指定不同的基类。

但是,如果 Something 直接从 ComponentBase 继承,则没有问题。

第一个问题请轻描淡写。如果需要,我会在早上更新代码和更多细节。

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: c# blazor blazor-server-side partial-classes


【解决方案1】:

使用代码隐藏文件,您可以在 2 个位置指定基类。

你将总是必须在 .razor 文件中指定它,因为你不希望那里有默认值:

// Something.razor
@inherits SomethingBase 

然后您可以在 .razor.cs 文件中进行选择:

// Something.razor.cs (a)
partial class Something      // take base class from other part
{
} 

// Something.razor.cs (b)
partial class Something : SomethingBase  // must use the same as other part
{
} 

我会使用 (a) 。

【讨论】:

  • 从那以后,对我来说并不明显的是 .razor 文件默认继承了一些东西,那就是 ComponentBase。所以默认情况下不需要@inherits ComponentBase,因为这是默认设置,但是当您更改继承背后的代码时,就会发生冲突。
  • 没错。 [填料填料]
猜你喜欢
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多