首页 > 解决方案 > Xamarin.Forms:颜色资源不适用于自定义控件的颜色属性

问题描述

我创建了一个具有图像和标签的自定义控件。我为它添加了一个可绑定的颜色属性。

 public static readonly BindableProperty TintColorProperty =
        BindableProperty.Create("TintColor", typeof(Color), typeof(Color), Color.Black,
            BindingMode.TwoWay, propertyChanged: OnTintColorChanged);
 public Color TintColor
 {
     get { return (Color)GetValue(TintColorProperty); }
     set { SetValue(ImageSourceProperty, value); }
 }

下面是属性更改事件。

private static void OnTintColorChanged(BindableObject bindable, object oldValue, object newValue)
{
     var control = bindable;
     ImageTint effect = (ImageTint)control.IconPng.Effects.FirstOrDefault(
         e => e is ImageTint);
     effect.TintColor = (Color)newValue;
}

当我从 xaml 传递系统颜色时,将调用该事件。但是当我通过颜色资源时,它没有!

TintColor="{StaticResource ColorPrimary}"

标签: xamarinxamarin.formscolors

解决方案


您的代码中几乎没有错误。特别是,您的属性设置器设置了与预期不同的属性。此外,您可能希望在编写绑定时将声明的类型更改为您的控件,以及删除双向绑定(我怀疑您是否需要它)。可能,至少在一种情况下它对您有用,这只是一个巧合。这是给你的一个样本。

自定义控件MyControl.xaml

    <?xml version="1.0" encoding="UTF-8" ?>
    <ContentView
        x:Class="App12.MyControl"
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:d="http://xamarin.com/schemas/2014/forms/design"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <ContentView.Content>
            <StackLayout>
                <Label x:Name="MyLbl" Text="Hello Xamarin.Forms!" />
            </StackLayout>
        </ContentView.Content>
    </ContentView>

自定义控件的代码隐藏MyControl.xaml.cs

    公共部分类 MyControl
        {
            公共静态只读 BindableProperty TintColorProperty =
                BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(MyControl), Color.Black,
                    propertyChanged: OnTintColorChanged);
    
            公共 MyControl()
            {
                初始化组件();
            }
    
            公共颜色 TintColor
            {
                获取{返回(颜色)GetValue(TintColorProperty);}
                设置 { SetValue(TintColorProperty, value); }
            }
    
            私有静态无效 OnTintColorChanged(BindableObject 可绑定,对象 oldValue,对象 newValue)
            {
                var control = 可绑定为 MyControl;
                如果(控制==空)
                {
                    返回;
                }
    
                control.MyLbl.TextColor = (颜色) newValue;
            }
        }

某些页面,您在其中使用控件

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage
        x:Class="App12.MainPage"
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:app12="clr-namespace:App12;assembly=App12"
        xmlns:d="http://xamarin.com/schemas/2014/forms/design"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <ContentPage.Resources>
            <ResourceDictionary>
                <Color x:Key="PrimaryTextColor">#ff00ff</Color>
            </ResourceDictionary>
        </ContentPage.Resources>
        <StackLayout>
            <app12:MyControl
                HorizontalOptions="Center"
                TintColor="{StaticResource PrimaryTextColor}"
                VerticalOptions="CenterAndExpand" />
            <app12:MyControl
                HorizontalOptions="Center"
                TintColor="Aqua"
                VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage>

推荐阅读