简体   繁体   English

如何将 string.Format 用于 @ref

[英]How to use string.Format for @ref

I have many dropdown items in my real code.我的真实代码中有很多下拉项。 Shown a sample below.下面显示了一个示例。 I am rewriting the code to reduce the number of lines, so that it is easier to read and maintain.我正在重写代码以减少行数,以便于阅读和维护。

Please see the old working code in the first section and the new code I have tried (at the bottom);请查看第一部分中的旧工作代码和我尝试过的新代码(在底部);

Old code:旧代码:

@if (Item.Contains("One"))
{
    <li class="dropdown-item">
        <button @ref="_btn1Ref" class="Scale" id="0" @onclick=OpenDialog1>Current</button>
    </li>
    <li class="dropdown-item">
        <button @ref="_btn2Ref" class="Scale" id="1" @onclick=OpenDialog2>UpHole Voltage</button>
    </li>
} 
                            
@if (Item.Contains("Two"))
{
    <li class="dropdown-item">
        <button @ref="_btn4Ref" class="Scale" id="3" @onclick=OpenDialog4>Head Tension</button>
    </li>
    <li class="dropdown-item">
        <button @ref="_btn5Ref" class="Scale" id="4" @onclick=OpenDialog5>Head Tension Temp</button>
    </li>
}

New cod - Added as per suggestion:新鳕鱼 - 根据建议添加:

List<string> name = new List<string>
                {"nothing", "one", "two", "three", "four"};
List<string> btnReference = new List<string>
            {"nothing", "_btn1Ref", "_btn2Ref", "_btn3Ref", 
             "_btn4Ref"
            };
List<string> openDialog = new List<string>
             {"nothing", "OpenDialog1", "OpenDialog2", "OpenDialog3", 
              "OpenDialog4"
            };

@for (var i = 1; i < 5; i++)
{
    bool isAvailable = false;  

    if (!isAvailable && NavItem.Contains("PH1"))
        isAvailable = i >= 1 && i <= 2;

    if (!isAvailable && NavItem.Contains("PH2"))
        isAvailable = i >= 3 && i <= 4;

    if (isAvailable)
    {
     <li class="dropdown-item">
            <button ref="@btnReference[i]" class="Scale" id="@i-1" onclick=@openDialog[i]>@tools[i]</button>
     </li>
    }
}

Can anyone guide me how I rewrite the above line.任何人都可以指导我如何重写上面的行。 Thank you.谢谢你。

You could store your references in a list and use them this way:您可以将引用存储在列表中并以这种方式使用它们:

@inject IJSRuntime JSRuntime

@for(int i=0; i < btnReferences.Count; i++)
{
  var iCopy = i;
  <button @ref="btnReferences[iCopy]" @onclick="(async () => await TestRef(iCopy))"></button>
}

@code{
  private List<ElementReference> btnReferences = new List<ElementReference>();

  protected override async Task OnInitializedAsync()
  {
    for(int i = 0; i < 5; i++)
    {
      btnReferences.Add(new ElementReference());
    }
  }

  public async Task TestRef(int i)
  {
    await JSRuntime.InvokeVoidAsync("console.log", testref[i].Id);
  }
}

As Brian already said, @ref needs ElementReference (or component type): string is not a valid type.正如 Brian 已经说过的,@ ElementReference @ref或组件类型): string不是有效类型。 @onclick meets the same issues. @onclick遇到同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM