I have created several CustomControls for using on my project. I have defined two projects:
- A
ClassLibrarywhich contains myCustomControls. - A
WinUIapplication which usesClassLibrary.
In the ClassLibrary I have created a custom control named Badge; Automatically visual studio defined a default Style with a sample Control Template for this Badge in Generic.xaml file under Themes folder.
I defined several Styles for this control. And I just put them into a file named BadgeStyle.xaml so I added this file as a MergedDictionary in Generic.xaml:
<ResourceDictionary xmlns:customControls="using:BSN.Kava.UI.SDK.CustomControls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///BSN.Kava.UI.SDK/CustomControls/BadgeStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="customControls:Badge" BasedOn="{StaticResource BadgeBase}" />
</ResourceDictionary>
I hoped that I'm able to use these Styles in my main WinUI3 application. But I can't. I should directly reference this BadgeStyle.xaml in App.xaml resource dictionary.
Surprisingly this line works (Without direct referencing to BadgeStyle.xaml in the app.xaml):
<Style TargetType="customControls:Badge" BasedOn="{StaticResource BadgeBase}" />
But the rest won't works.
Where is my problem?
Let's say you have a control library project called CustomControlsLibrary and your WinUI 3 app project has a reference to that library project.
And in CustomControlsLibrary, you have a custom control CustomItem. Usually, you would have its template on:
CustomControlsLibrary/Themes/Generic.xaml
You also have additional styles for CustomItem in a seperate file:
CustomControlsLibrary/Themes/CustomItemStyles.xaml
In order to use the additional styles in you WinUI 3 app project, you need to declare it on App.xaml.
App.xaml
Then, you'll be able to use it:
UPDATE
If you are placing your additional styles in Generic.xaml: