<MudIcon Icon="@inboxIcon" Color="Color.Primary" />@code{
// here this Icon string is coming from database
private string inboxIcon = "Icons.Material.Filled.Inbox";
}上面的代码没有显示任何图标。如何绑定这个图标字符串?
发布于 2022-05-10 19:03:01
以前的问题,但我也试图保存/加载菜单图标从数据库,并结束使用反射,如下面的例子。我想使用的所有图标都在Icons.Material.Outlined中,所以我只是保存图标名"Person“、"Dashboard”等等(以及路径和标题),以便在运行时加载。这个样本可能会让你:
@using System.Reflection
<MudNavMenu>
@foreach (var menuitem in MenuItems)
{
<MudNavLink Href="@menuitem" Icon="@GetIconValue(Icons.Material.Outlined, @menuitem)">@menuitem</MudNavLink>
}
</MudNavMenu>
@code {
string[] MenuItems = new string[] {"Dashboard", "Person"};
public static string GetIconValue(object SourceField, string IconName)
{
return SourceField.GetType().GetProperty(IconName).GetValue(SourceField, null).ToString();
}
}在Mudblazor上运行:https://try.mudblazor.com/snippet/cuQQuplkqVtQpJKV
发布于 2021-12-21 10:30:04
你就快到了。试着这样做:
<MudIcon Icon="@inboxIcon" Color="Color.Primary" />
@code{
// here this Icon string is coming from database
private string inboxIcon = MudBlazor.Icons.Material.Filled.Inbox;
}命名空间中的Icons将自动转换为字符串。
https://stackoverflow.com/questions/70265027
复制相似问题