阵列有没有办法容纳blazor组件



我目前设置了一个剃刀文件,如下所示:

@switch (currentStep)
{
case 0:
<Component1 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />
break;
case 1:
<Component2 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward" />
break;
case 2:
<Component3 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"/>
break;
case 3:
<Component3/>
break;
default:
<p>Some error message</p>
break;
}
</div>`
@code {
private int currentStep = 0;
private void MoveForward()
{
if (currentStep < 3) currentStep += 1;
}
private void MoveBackward()
{
if (currentStep > 0) currentStep -= 1;
}
}

我在哪里增加";currentStep";在每个组件内部使用moveforward/movebackward作为回调来显示下一个组件。我想知道,我是否可以创建一个只包含所有组件的数组,然后调用该数组,而不是switch语句?像下面这样的东西?

<@steps[currentStep]>
@code{
private __[] steps = new String[]
{
<component1 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />
<component2 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />
<component3 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />
<component4 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />
}
}

我只是不确定如果可能的话,数组应该有什么类型,以及如何创建该类型。

任何帮助都将不胜感激,谢谢!

另一种方法是将组件标记存储在RenderFragment数组中,这与您的想法略有不同:

@steps[currentStep] 
@code{
private RenderFragment[] steps;
private int currentStep = 0;
protected override void OnInitialized()
{
steps = new RenderFragment[4];
steps[0] = @<component1 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />;
steps[1] = @<component2 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />;
steps[2] = @<component3 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />;
steps[3] = @<component4 MoveForwardCallback="MoveForward" MoveBackwardCallback="MoveBackward"  />;
}
private void MoveForward()
{
if (currentStep < 3) currentStep += 1;
}
private void MoveBackward()
{
if (currentStep > 0) currentStep -= 1;
}
}

样品:https://blazorrepl.com/repl/wvuoOtYb026k7nQj39

相关内容

  • 没有找到相关文章

最新更新