如何为 Xamarin.forms 中的颜色使用可绑定的 属性?
How to use a bindable property for colours in Xamarin.forms?
我有自己的带有一些可绑定属性的自定义控件。我使用 YouTube 非常轻松地编写了字符串可绑定属性。
但是如何使用颜色的可绑定属性?
您可以参考以下代码:
自定义控件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
public partial class MyControl
{
public static readonly BindableProperty TintColorProperty =
BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(MyControl), Color.Black,
propertyChanged: OnTintColorChanged);
public MyControl()
{
InitializeComponent();
}
public Color TintColor
{
get { return (Color) GetValue(TintColorProperty); }
set { SetValue(TintColorProperty, value); }
}
private static void OnTintColorChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = bindable as MyControl;
if (control == null)
{
return;
}
control.MyLbl.TextColor = (Color) 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>
我有自己的带有一些可绑定属性的自定义控件。我使用 YouTube 非常轻松地编写了字符串可绑定属性。
但是如何使用颜色的可绑定属性?
您可以参考以下代码:
自定义控件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
public partial class MyControl
{
public static readonly BindableProperty TintColorProperty =
BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(MyControl), Color.Black,
propertyChanged: OnTintColorChanged);
public MyControl()
{
InitializeComponent();
}
public Color TintColor
{
get { return (Color) GetValue(TintColorProperty); }
set { SetValue(TintColorProperty, value); }
}
private static void OnTintColorChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = bindable as MyControl;
if (control == null)
{
return;
}
control.MyLbl.TextColor = (Color) 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>