如何使用Style Inheritance有多个文件吗?我已经能够利用Stand-alone resource dictionaries但如果子样式BasedUpon
是另一个文件中的样式,则它不起作用。
在下面的示例中,ListView
将具有指定的 SeparatorColor
,但没有来自 BasedUpon
的 BackgroundColor
风格。
BaseStyles.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Color x:Key="LightPrimaryColor">LightPink</Color>
<Color x:Key="DarkPrimaryColor">DarkGreen</Color>
<Style x:Key="BaseColorStylePrimary" TargetType="View">
<Setter Property="BackgroundColor"
Value="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" />
</Style>
</ResourceDictionary>
SomeChildStyles.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<Style x:Key="ListViewStyleBasedOn"
TargetType="ListView"
BasedOn="{StaticResource BaseColorStylePrimary}">
<Setter Property="SeparatorColor"
Value="{AppThemeBinding Light=Orange, Dark=Red}" />
</Style>
</ResourceDictionary>
App.xaml
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="StyleInheritance.App">
<Application.Resources>
<ResourceDictionary Source="BaseStyles.xaml" />
<ResourceDictionary Source="SomeChildStyles.xaml" />
</Application.Resources>
</Application>
看起来他们 made some changes a few years ago实现我想要做的事情,但是当 they introduced AppThemeBinding
它没有被扩展来处理这个🤔?
最佳答案
- 添加一个代码隐藏文件并调用
InitializeComponent()
即使 this blog post 到每个单独的资源文件说不再需要。 - 请勿使用 fancy clean way删除用于定义
<ResourceDictionary>
的额外代码的
更新了BaseStyles.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<!--change is here-->
x:Class="StyleInheritance.BaseStyles">
<Color x:Key="LightPrimaryColor">LightPink</Color>
<Color x:Key="DarkPrimaryColor">DarkGreen</Color>
<Style x:Key="BaseColorStylePrimary" TargetType="View">
<Setter Property="BackgroundColor"
Value="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" />
</Style>
</ResourceDictionary>
BaseStyles.xaml.cs
using Xamarin.Forms;
namespace StyleInheritance
{
public partial class BaseStyles : ResourceDictionary
{ public BaseStyles() { InitializeComponent(); } }
}
SomeChildStyles.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<!--change is here-->
x:Class="StyleInheritance.SomeChildStyles">
<Style x:Key="ListViewStyleBasedOn"
TargetType="ListView"
BasedOn="{StaticResource BaseColorStylePrimary}">
<Setter Property="SeparatorColor"
Value="{AppThemeBinding Light=Orange, Dark=Red}" />
</Style>
</ResourceDictionary>
添加了SomeChildStyles.xaml.cs
using Xamarin.Forms;
namespace StyleInheritance
{
public partial class SomeChildStyles : ResourceDictionary
{ public SomeChildStyles() { InitializeComponent(); } }
}
App.xaml
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="StyleInheritance.App">
<Application.Resources>
<!--change is here-->
<ResourceDictionary>
<ResourceDictionary Source="BaseStyles.xaml" />
<ResourceDictionary Source="SomeChildStyles.xaml" />
</ResourceDictionary>
</Application.Resources>
</Application>
我已经创建了完整的 Sample project自述文件中包含一些其他详细信息。
关于xamarin - 基于跨多个文件的其他样式的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63379872/