【发布时间】:2020-12-23 20:34:36
【问题描述】:
我正在使用一个自定义基组件类,每个自定义页面都从中继承。
我没有任何剃须刀代码,因为它不会在我的基本组件类中进行模板化 - 它只应该包含一些基本逻辑和初始化内容。
现在我想包装一个级联值,因为在任何将嵌套在任何子级别中的组件/页面上都应该可以访问我的基类的属性值。我该怎么做,我试图在基本组件上伪造一些 childContent 但我没有工作
public class BaseComponent : ComponentBase
{
[Parameter] public RenderFragement ChildContent {get;set;}
protected override void OnInitialized()
{
ChildContent = b =>
{
b.OpenComponent<CascadingValue<IBvdDataComponent>>(0);
b.AddAttribute(1, "Name", "DataComponent");
b.AddAttribute(2, "Value", this);
b.CloseComponent();
};
}
}
My page component:
@page "/anypage"
@inherits BaseComponent
<div>
<MyNestedComponent />
</div>
在 MyNestedComponent 我想做的事情:
[CascadingParameter]
public BaseComponent BaseComponent { get; set; }
这样做的正确方法是什么。
【问题讨论】:
-
我不认为你会用这种方法成功 - github.com/dotnet/aspnetcore/issues/22411
-
不完全是我正在寻找的,因为他们大多使用剃刀语法来获取围绕其基本组件的 CascadingValue。我尝试动态注入它。我尝试按照 chrissinty link 的指示进行操作。但我没有成功。 buildRenderTree 方法永远不会被调用 - 我不知道为什么
-
因为当你从一个组件下降时,它会覆盖 BuildRenderTree。
-
好吧 - 这是有道理的。我不知道 BuildRenderTree 被完全覆盖
-
如果您查看 obj 文件夹,您可以找到为您的 razor 文件生成的 CS 文件。