问题
How do I use merged styles? I am merging in application file then I want to use it on another screen, but I don't find the style, what is my wrong?
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns=...
x:Class="CodeFabric.ExpenseTracker.Mobile.Forms.Styles.EntryStyle">
<Style x:Key="highlightedLabel" TargetType="Label">
<Setter Property="TextColor" Value="White" />
<Setter Property="BackgroundColor" Value="LightGreen" />
</Style>
</ResourceDictionary>
I am merging here: until here all is working fine
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary MergedWith="local:EntryStyle"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
but when I am using the style here, the style isn't found. Why?
<Label Style="{StaticResource highlightedLabel}" Text="I'm Highlighted" />
回答1:
The 'MergedWith' property is obsolete now, see https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.resourcedictionary.mergedwith?view=xamarin-forms
Basically, you can use merged dictionaries with a standalone resource file like this:
<Application ...
xmlns:local="clr-namespace:ResourceDictionaryDemo">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:MyResourceDictionary />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
...
</Application>
More usage please refer to official doc
Hope it helps.
回答2:
Try this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:EntryStyle />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
hope this helps.-
来源:https://stackoverflow.com/questions/60621436/how-use-the-styles-merged