How use the styles merged?

守給你的承諾、 提交于 2020-03-25 18:48:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!