Hello,
Welcome to Microsoft Q&A!
The first thing needs to know is that when you use the Resource
property of the framework element, the element implicitly creates a resource dictionary for you. Like this:
<StackPanel.Resources>
<x:String x:Key="greeting">Hello world</x:String>
</StackPanel.Resources>
But when you want to add merged dictionaries with other styles, you will need to do it a little bit different. You could declare <ResourceDictionary>
first, then add things to its <ResourceDictionary.MergedDictionaries>
collection. After <ResourceDictionary.MergedDictionaries>…</ResourceDictionary.MergedDictionaries>
, you can optionally put additional resources in your main dictionary.
So, the code should be like this for your first question, you don't need to add a key for the ResourceDictionary
. And you won't worry for the second question, just call the styles as usual.
<RelativePanel.Resources>
<!--This works-->
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button" x:Key="Default">
<Setter Property = "Margin" Value="10"/>
</Style>
</ResourceDictionary>
</RelativePanel.Resources>
For more details about ResourceDictionary
, please refer to this document: ResourceDictionary and XAML resource references
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.