【问题标题】:How to use a bindable property for colours in Xamarin.forms?如何在 Xamarin.forms 中为颜色使用可绑定属性?
【发布时间】:2021-08-25 02:15:58
【问题描述】:

我有自己的自定义控件,带有一些可绑定的属性。我使用 YouTube 非常轻松地编写了字符串可绑定属性。

但是如何使用颜色的可绑定属性呢?

【问题讨论】:

    标签: xamarin.forms colors bindableproperty


    【解决方案1】:

    可以参考以下代码:

    自定义控件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>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    • 2021-11-10
    • 2010-12-03
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多