xamarin - 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}"
解决方案
您的代码中几乎没有错误。特别是,您的属性设置器设置了与预期不同的属性。此外,您可能希望在编写绑定时将声明的类型更改为您的控件,以及删除双向绑定(我怀疑您是否需要它)。可能,至少在一种情况下它对您有用,这只是一个巧合。这是给你的一个样本。
自定义控件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>
推荐阅读
- python - 随机森林回归超参数调整降低了测试集的性能
- gpu - OpenVINO 可以支持(和使用)Nvidia GPU 吗?
- c - 如何在输入两次之前输入?
- .net - Log4net adonetappender 在 .net 5 中不起作用
- json - 如何在 Spring Boot REST API 中将空值包含到 JSON 响应中
- javascript - ReferenceError:未定义星星
- javascript - 如何在 Ace Editor 的滚动条上标记线条
- json - 使用 Visual Basic 在电子表格中编写代码
- c# - 铸造通用委托动作
行动 - reactjs - 使用 Enzyme 和 Jest 测试 Material-UI Select 控件