by Anonymous » 16 Feb 2025, 15:09
Grundsätzlich versuche ich, eine Mischung aus einer Vorlagenkomponente und einem Layout zu erstellen. > Parameter zu. Ich denke, ein Weg, dies zu tun, kann darin bestehen, eine abstrakte Basisklasse zu nutzen. Ich möchte das nicht tun. Ich bin damit einverstanden, dass die Basis durch einen Renderbaum (d. H. Option 3) erstellt wird, aber nicht die Komponente, die daraus erbt (d. H. MyGrid.razor )
Was ich tun möchte
Code: Select all
@page "/mygrid"
@inherits GridPage @*or "layout" or whatever works*@
@code {
protected overrides string Title => "My Grid"
}
< /code>
GridPage.cs
Code: Select all
@Title
@ChildContent
@code {
[Parameter] public string Title { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
}
< /code>
[b]Solutions I have tried[/b]
[list]
[*]Layout - GridLayout
[/list]
Code: Select all
@Title
@Body
@code {
[CascadingParameter]
[Parameter] public string Title { get; set; }
}
< /code>
Issue is that you have to remember this param is available and it is not required to implement
[list]
[*]Template
[/list]
< /code>
Issue here is that I would have to wrap my component with the template (not that big of deal, but would rather inherit) and I still don't know which params are required to implement.
[list]
[*]Abstract base class
[/list]
public abstract class GridPage : ComponentBase
{
protected abstract string Title { get; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenRegion(0);
builder.OpenElement(1, "header");
builder.AddContent(2, Title);
builder.CloseElement();
builder.CloseRegion();
builder.OpenElement(3, "div")
builder.OpenRegion(10);
this.BuildRenderTree(builder);
builder.CloseRegion();
builder.Closelement()
}
}
< /code>
Issue is this one doesn't seem to work. I actually thought it would cause it would just take in the component and continue the base render (this.BuildRenderTree(builder);
). Raster und Klassen Grid. Ich würde für jeden von ihnen ein anderes Raster (und einen Titel) wollen. Ich möchte den Titel und die Spalten für jeden im Grunde genommen im Grunde genommen anpassen. Sie hätten ein gemeinsames Layout -Markup (was ich GridPage nenne), aber MyGrid (entweder Schüler oder Klassen ) unterschiedlich.
Code: Select all
@page "/students"
@inherits GridPage
@code {
protected overrides string Title => "Students"
}
< /code>
Classes.razor
Code: Select all
@page "/classes"
@inherits GridPage
@code {
protected overrides string Title => "Classes"
}
< /code>
They would both output
Title_here
Grid_markup_here
Grundsätzlich versuche ich, eine Mischung aus einer Vorlagenkomponente und einem Layout zu erstellen. > Parameter zu. Ich denke, ein Weg, dies zu tun, kann darin bestehen, eine abstrakte Basisklasse zu nutzen. Ich möchte das nicht tun. Ich bin damit einverstanden, dass die Basis durch einen Renderbaum (d. H. Option 3) erstellt wird, aber nicht die Komponente, die daraus erbt (d. H. MyGrid.razor )
[b] Was ich tun möchte [/b]
[code]MyGrid.razor[/code]
[code]@page "/mygrid"
@inherits GridPage @*or "layout" or whatever works*@
@code {
protected overrides string Title => "My Grid"
}
< /code>
GridPage.cs[/code]
[code]@Title
@ChildContent
@code {
[Parameter] public string Title { get; set; }
[Parameter] public RenderFragment ChildContent { get; set; }
}
< /code>
[b]Solutions I have tried[/b]
[list]
[*]Layout - GridLayout[/code]
[/list]
[code]@Title
@Body
@code {
[CascadingParameter]
[Parameter] public string Title { get; set; }
}
< /code>
Issue is that you have to remember this param is available and it is not required to implement
[list]
[*]Template
[/list]
< /code>
Issue here is that I would have to wrap my component with the template (not that big of deal, but would rather inherit) and I still don't know which params are required to implement.
[list]
[*]Abstract base class
[/list]
public abstract class GridPage : ComponentBase
{
protected abstract string Title { get; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenRegion(0);
builder.OpenElement(1, "header");
builder.AddContent(2, Title);
builder.CloseElement();
builder.CloseRegion();
builder.OpenElement(3, "div")
builder.OpenRegion(10);
this.BuildRenderTree(builder);
builder.CloseRegion();
builder.Closelement()
}
}
< /code>
Issue is this one doesn't seem to work. I actually thought it would cause it would just take in the component and continue the base render (this.BuildRenderTree(builder);[/code]). Raster und Klassen Grid. Ich würde für jeden von ihnen ein anderes Raster (und einen Titel) wollen. Ich möchte den Titel und die Spalten für jeden im Grunde genommen im Grunde genommen anpassen. Sie hätten ein gemeinsames Layout -Markup (was ich GridPage nenne), aber MyGrid (entweder Schüler oder Klassen ) unterschiedlich.
[code]Students.razor[/code]
[code]@page "/students"
@inherits GridPage
@code {
protected overrides string Title => "Students"
}
< /code>
Classes.razor[/code]
[code]@page "/classes"
@inherits GridPage
@code {
protected overrides string Title => "Classes"
}
< /code>
They would both output
Title_here
Grid_markup_here
[/code]