xamarin - 基于跨多个文件的其他样式的样式

标签 xamarin xamarin.forms

如何使用Style Inheritance有多个文件吗?我已经能够利用Stand-alone resource dictionaries但如果子样式BasedUpon是另一个文件中的样式,则它不起作用。

在下面的示例中,ListView 将具有指定的 SeparatorColor,但没有来自 BasedUponBackgroundColor风格。

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它没有被扩展来处理这个🤔?

最佳答案

  1. 添加一个代码隐藏文件并调用 InitializeComponent()即使 this blog post 到每个单独的资源文件说不再需要。
  2. 请勿使用 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/

相关文章:

Xamarin Forms Bind Array 元素在 listView 内

xaml - 当内容不可见时隐藏列或行

c# - 从 Xamarin CrossMedia 插件将文件上传到服务器

ios - 如何从masterdetailpage中的detailpage推送异步?

android - Xamarin Android - 移植 Android Api 26 --> API 28 - 单击时按钮置于前面

git - git下的Resource.Designer.cs

android - XML dp 不同于计算的 dp (dip)

c# - 相对布局 (XAML) 中的布局项

xamarin - 汉堡包菜单 Xamarin Forms (MasterDetailPage)

c# - xamarin.forms 中开关为 "on"(蓝色到绿色)时如何更改默认颜色在 ios 中默认为绿色,但在 android 中为蓝色