docs: add how to set an icon dynamically
🙋 Documentation Request
It would be nice if the documentation can show on how to set a icon dynamically.
💁 Possible Solution
I am thinking of something like that.
razor page:
<FluentIcon ref="@myIcon" Icon="@Icons.Filled.Size24.WeatherSunny" OnClick="@changeIcon" />
@code {
private FluentIcon<Icon>? myIcon {get; set;}
private viod changeIcon() {
myIcon.Icon = Icons.Filled.Size24.WeatherMoon;
}
}
Yes. It's a good idea. I'll do that.
In summary, the new FluentIcon component has 2 attributes for displaying a specific icon:
- Icon is a Type representing the icon class that will be instantiated by the component.
- Value is an Icon object that will be used by the component.
Yes. It's a good idea. I'll do that.
In summary, the new FluentIcon component has 2 attributes for displaying a specific icon:
- Icon is a Type representing the icon class that will be instantiated by the component.
- Value is an Icon object that will be used by the component.
Thank you for the explanation. But how can i dynmically assign an icon to a <FluentIcoin > Tag?
Thank you for the explanation. But how can i dynmically assign an icon to a
<FluentIcoin >Tag?
By setting the Value parameter, i.e.:
<FluentIcon Value="Icons.GetInstance(item)" Color=IconColor Title="@item.Name" /> where item is of Type IconInfo
Or use your own custom image by calling the Icon.FromImageUrl method:
<FluentSystemIcon Value="@(Icon.FromImageUrl("/Blazor.png"))" Width="32px" />
Thank you for the explanation. But how can i dynmically assign an icon to a
<FluentIcoin >Tag?By setting the
Valueparameter, i.e.:<FluentIcon Value="Icons.GetInstance(item)" Color=IconColor Title="@item.Name" />whereitemis of TypeIconInfoOr use your own custom image by calling the
Icon.FromImageUrlmethod:<FluentSystemIcon Value="@(Icon.FromImageUrl("/Blazor.png"))" Width="32px" />
Man, this is awsome!
I did it now like that:
example.razor:
<FluentIcon Value="Icons.GetInstance(dmIcon)" Color="Color.Accent" OnClick="@changeColorMode" class="dmIcom" />
example.razor.cs
public partial class Example
{
private IconInfo dmIcon = new IconInfo { Name = "WeatherSunny", Size = IconSize.Size24, Variant = IconVariant.Filled };
protected override async Task OnParametersSetAsync() {
if(darkMode) {
dmIcon = new IconInfo { Name = "WeatherMoon", Size = IconSize.Size24, Variant = IconVariant.Filled };
} else {
dmIcon = new IconInfo { Name = "WeatherSunny", Size = IconSize.Size24, Variant = IconVariant.Filled };
}
}
}
The important hint for me was Icons.GetInstance!
Many thanks, that is working like a charm.
How would one do this, as of v4.11.0, since Icons no longer has a GetInstance() method?
@{
var myIconName = "ChevronRight";
var iconInfo = new IconInfo() { Name = myIconName, Size = IconSize.Size20, Variant = IconVariant.Regular };
var myIcon = Icons.GetInstance(iconInfo);
<FluentIcon Value="@myIcon" />
}
Produces this error message:
The type or namespace name 'GetInstance' does not exist in the namespace 'Microsoft.FluentUI.AspNetCore.Components.Icons' (are you missing an assembly reference?)
There is always a GetInstance method in the IconsExtensions class.
See https://github.com/microsoft/fluentui-blazor/blob/main/src/Core/Components/Icons/IconsExtensions.cs#L28
Thank you, works like a charm!