MartinTopfstedt/FontAwesome6

How to use FontAwesome6.Pro.Svg

damian4891 opened this issue · 4 comments

Hello!

The FontAwesome6.Pro.Svg "Setup" of your documentation steps seems to be missing, the Setup only exist for Fonts.
I'm using WPF and have correctly generated the json file, and use the following to load the SVGs:

FontAwesome6.Svg.FontAwesomeSvg.LoadFromDirectory(ProgramConstants.FONT_AWESOME_FILE);

This works when the program is compiled, but when designing and building my XAML UI, I receive dozens of errors: "Couldn't load icon "ICON NAME". Please load the svg data for that icon first". As the icons are only loaded after compile, I understand the error. But what is the solution?

Perhaps using FontAwesome6.Svg.FontAwesomeSvg.LoadFromResource() will solve this problem, but I can't get this to work.

I've added the json as both Embedded Resource, Resource and even Content, but and am using:
FontAwesome6.Svg.FontAwesomeSvg.LoadFromResource("/Resources/FontAwesome6Svg.json", System.Reflection.Assembly.GetExecutingAssembly());

But that line of code still generates the following error on compile:
System.ArgumentNullException: 'Value cannot be null. Parameter name: stream'

Thanks for your help!

Hi, if you use LoadFromResource the Resource Name is the Namespace + fileName, it is not a path.
e.x. Svg.FontAwesomeSvg.LoadFromResource("FontAwesome6.Example.WPF.Svg.FontAwesomeSvg.all.json", Assembly.GetExecutingAssembly());
"FontAwesome6.Example.WPF" < this is the default Namespace from the "FontAwesome6.Example.WPF.NetCore" project.
Inside the project there is a "Svg" Folder which contains the "FontAwesomeSvg.all.json" file as embedded resource.
-> "{Namespace}.{Path inside Project with . instead of /}.{FileName}"

Hope this helps. I will try to make the README complete.

Ok, i played a little bit around and the problem here is that the json file needs to be loaded, directory or embedded resource doesn't matter. I could get it working by adding the Svg.FontAwesomeSvg.LoadFromResource inside the constructor of the ViewModel of a Window and set the ViewModel as DataContext inside the Window,xaml. But this is not really a good solution

Hi, i found a way to load the icons, but it is very hacky :).
You can add a DesignTimeResourceDictionary and load a dedicated Loading UserControl which calls the load method.
I added a tutorial to the README and modified the Example Projects.

gakera commented

I would say that the Readme could be updated with better instructions for how to do the runtime setup to get things working.

For example, to get FontAwesome6.Pro.Svg working in my WinUI 3 project, I have to:

  1. (follow the instructions on https://github.com/MartinTopfstedt/FontAwesome6#fontawesome6prosvg as written to install the nuget package and generate the json file)
  2. In csproj file, make sure json file is included as embedded resource (I include it as linked since it's shared between a couple of projects) <EmbeddedResource Include="..\Shared\Assets\FontAwesomeSvg.json" Link="SharedAssets\FontAwesomeSvg.json"/>. I included the "all.json" file from the generator, just renamed.
  3. In startup App constructor, after configuring services, with using FontAwesome6.Svg;, run FontAwesomeSvg.LoadFromResource with the appropriate resource string. To find the appropriate resource string I used System.Reflection.Assembly.GetCallingAssembly().GetManifestResourceNames() to see the name and then FontAwesomeSvg.LoadFromResource("MyProjectName.SharedAssets.FontAwesomeSvg.json", System.Reflection.Assembly.GetCallingAssembly());
  4. In XAML, include xmlns:fas="using:FontAwesome6.Svg" (not xmlns:fas="http://schemas.fontawesome.com/icons/svg, that doesn't work) and then use <fas:SvgAwesome Icon="Light_Eject" PrimaryColor="HotPink" Width="50"/> for the icon as needed.

This seems to be sufficient to get FontAwesome6.Pro.Svg working in my WinUI 3 project.