【问题标题】:Standardize Xamarin Android Styles.xml and ResourceDictionary标准化 Xamarin Android Styles.xml 和 ResourceDictionary
【发布时间】:2022-01-27 00:50:09
【问题描述】:

我试图避免在 android styles.xmlresourcedictionary 中重复声明样式定义。例如,

在 Android styles.xml 中,我有以下更改默认主题的原色

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="MyDefaultTheme">
        <item name="colorPrimary">@color/colorPrimary</item>
    </style>
    
    <color name="colorPrimary">#0E80FF</color>
</resources>

DefaultResourceDictionary.xaml 中,我有以下内容,以便我的XF 控件使用与styles.xml 中定义的颜色相同的颜色:

<ResourceDictionary ...>
    <Color x:Key="colorPrimary">#0E80FF</Color>
</ResourceDictionary>

在我的脑海中,我可以想到做一些PreBuild 操作来调用XmlPoke 以将来自ResourceDictionary 的值插入styles.xml

有没有更简单的方法?

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android


    【解决方案1】:

    是的,您可以在 App 类中添加样式。 在 App.xaml 文件中添加此示例

     <Application.Resources>
            <ResourceDictionary>
    
                <Color x:Key="PrimaryColor">#512bdf</Color>
                <Color x:Key="SecondaryColor">White</Color>
    
                <Style TargetType="Label">
                    <Setter Property="TextColor" Value="{DynamicResource PrimaryColor}" />
                    <Setter Property="FontFamily" Value="OpenSansRegular" />
                </Style>
    
                <Style TargetType="Button">
                    <Setter Property="TextColor" Value="{DynamicResource SecondaryColor}" />
                    <Setter Property="FontFamily" Value="OpenSansRegular" />
                    <Setter Property="BackgroundColor" Value="{DynamicResource PrimaryColor}" />
                    <Setter Property="Padding" Value="14,10" />
                </Style>
    
            </ResourceDictionary>
        </Application.Resources>
    

    【讨论】:

    • 没有解决我的问题。是的,我可以在我的App.xaml 中添加PrimaryColor,但我仍然需要在styles.xml 中为android 有一个重复的条目
    • but I will still need to have a duplicate entry in styles.xml for android 抱歉,我不太明白你的意思。你想达到什么目标?你能详细说明一下吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多