-
Notifications
You must be signed in to change notification settings - Fork 231
Open
Labels
Description
- NET Version: 10.0.100
So I am playing with Blazor a little bit and I encountered strange behaviour:
I have a Text component:
@ChildContent
@X
@code {
[Parameter]
public RenderFragment? ChildContent { get; set; }
[Parameter]
public string X { get; set; } = "";
}and an App component:
<Text>@_text</Text>
@if (_x)
{
<Text>General Kenobi!</Text>
}
else
{
<Text>Goodbye, world!</Text>
}
@code {
private string _text = "Hello, world!";
private bool _x = false;
protected override async Task OnInitializedAsync()
{
await Task.Delay(4000);
_text = "Hello there!";
_x = true;
}
}This is decompiled high-level C# for this component:
#nullable enable
namespace BlazorTuiTests.Components;
public class App : ComponentBase
{
private string _text = "Hello, world!";
private bool _x = false;
protected override void BuildRenderTree(
#nullable disable
RenderTreeBuilder __builder)
{
__builder.OpenComponent<Text>(0);
__builder.AddAttribute(1, "ChildContent", (MulticastDelegate) (__builder2 => __builder2.AddContent(2, this._text)));
__builder.CloseComponent();
if (this._x)
__builder.AddContent(3, "General Kenobi!");
else
__builder.AddContent(4, "Goodbye, world!");
}
protected override async
#nullable enable
Task OnInitializedAsync()
{
await Task.Delay(4000);
this._text = "Hello there!";
this._x = true;
}
}Notice that inside of if statement it's calling AddContent instead of creating component. I went further with that and used parameter X on my Text component. Outside the if statement it works, but if I do it inside, it generates compilation error:
Error RZ1023 : "<text>" and "</text>" tags cannot contain attributes.
@* This works *@
<Text X="something">@_text</Text>
@if (_x)
{
// This doesn't
<Text X="something">General Kenobi!</Text>
}It looks like inside an if statement, Razor code gets compiled as it was text markup? If I change Text component name to, for example, MyText then the problem goes away.