andreinitescu / IconFont2Code

Generate C# class with constant fields for the icons in your font
277 stars 37 forks source link

Add support for generating ResourceDictionary XAML #18

Closed kcrg closed 1 year ago

kcrg commented 1 year ago

Something like that:

image

andreinitescu commented 1 year ago

Hey Kacper, thanks a lot for creating this issue, it's interesting. What is the advantage of using a ResourceDictionary? With a ResourceDictionary, how do you reference the keys (PersonIcon)? Can you show an example please? Thanks!

kcrg commented 1 year ago

Hi @andreinitescu!

You just need to add this single file FontIcons.xaml ResourceDictionary (without codebehind, I think with codebehind this can work too but this time I create this that way) to MergedDictionaries in App.xaml. image

And then just reference keys like any other StaticResource: image

andreinitescu commented 1 year ago

Thanks! My question was, why do you prefer using a ResourceDictionary instead of just using static class, like this:

<Glyph="{x:Static myApp:IconFont.IconAlarm}"

I mean, is it just a matter of preference? Or does using a ResourceDictionary have some advantages in some way? (just an example, with a static class, Visual Studio still provide auto complete like in your screen shot).

andreinitescu commented 1 year ago

The only advantage I can think of is, with resources, you could dynamically switch them at runtime. Like, the app wants to be able to dynamically replace icons at run-time, like an editor app. In this scenario a resource helps a lot, but you would have to use DynamicResource and not a StaticResource.

kcrg commented 1 year ago

It's just a matter of preference, I think it's a little cleaner to use StaticResource.

andreinitescu commented 1 year ago

@kcrg ✅ DONE! Take it for a spin please 🎉🎈 Let me know what you think, and thanks for taking the time to open the issue.

andreinitescu commented 1 year ago

A little observation: A ResourceDictionary is actually a Dictionary, therefore for every entry you put in XAML there's a call at runtime to add the key and value to the dictionary.

So I think it would be better not put 1,000 entries in the XAML for all the icons in the font. Instead, click on the glyphs you need in the app, and the tool will generate the code only for those.

kcrg commented 1 year ago

@andreinitescu It looks like everything is working great, thanks for your time