标准化Xamarin Android Styles.xml和ResourceDictionary



我正在努力避免在androidstyles.xmlresourcedictionary中重复声明样式定义。例如,

在Androidstyles.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中。

还有什么不那么复杂的方法吗?

是的,您可以在应用程序类中添加样式。在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>

相关内容

  • 没有找到相关文章

最新更新