首页 > 解决方案 > 自定义控件不更新背景颜色 Xamarin Forms

问题描述

我创建了一个自定义控件,我想在单击后更改颜色。我可以成功加载它,但是一旦我运行我的函数并想要更改颜色,什么也没有发生。当我尝试使用常规元素(按钮、标签)进行操作时,它可以工作。

自定义控件(即内部带有简单标签的 Grid):

public partial class CustomGrid : Grid
{
    public static readonly BindableProperty ItemProperty = BindableProperty.Create(nameof(Item), typeof(Item), typeof(CustomGrid), propertyChanged: ItemChanged);
    public static readonly BindableProperty CustomColorProperty = BindableProperty.Create(nameof(IsAnonymousSubColor), typeof(Color), typeof(CustomGrid), propertyChanged: CustomColorChanged);

    public Color CustomColor
    {
        get { return (Color)GetValue(CustomColorProperty); }
        set { SetValue(CustomColorProperty, value); }
    }

    static void CustomColorChanged (object bindable, object oldValue, object newValue)
    {
        var x = bindable as CustomGrid;

        if (x.Item != null)
        {
             x.myLabelInMyXaml.BackgroundColor = x.CustomColor;
        }
    }

    static void ItemChanged (object bindable, object oldValue, object newValue)
    {
        var x = bindable as CustomGrid;
    }

    public Item Item
    {
        get { return (Item) GetValue(ItemProperty); }
        set { SetValue(ItemProperty, value); }
    }

    public CustomGrid()
    {
        InitializeComponent();
    }
}

这是我的 xaml,在这里我将它绑定到 viewmodel 颜色代码(绑定在其他元素上效果很好,即使我在同一个 stacklayout 中尝试它)。

                <StackLayout BackgroundColor="Transparent"
                             Padding="0,10,0,0"
                                 VerticalOptions = "StartAndExpand" 
                                BindableLayout.ItemsSource="{Binding newsService.FeedList}">
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <StackLayout>

                                <controls:CustomGrid Item ="{Binding .}"
                                               CustomColor = "{Binding BindingContext.CustomColorViewModel, Source={x:Reference ParentView}}" />

                            </StackLayout>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </StackLayout>

视图模型:

    private Color _CustomColorViewModel;
    public Color CustomColorViewModel
    {
        get { return _CustomColorViewModel; }
        set
        {
            _CustomColorViewModel = value;
            RaisePropertyChanged("CustomColorViewModel");
        }
    }

我在加载后更改它:

public void ChangeColor ()
{
    CustomColorViewModel = Color.Red;
}

自定义控件的 XAML:

  <?xml version="1.0" encoding="UTF-8"?>
  <Grid xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:Neutral.Controls"
         xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
         xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
         xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
         x:Name="ParentView"
         x:Class="Project.Controls.CustomGrid">


         <Label x:Name = "myLabelInMyXaml" BackgroundColor = "{Binding CustomColorViewModel}"/>


 </Grid>

但自定义控件背景颜色不会改变。它在我最初加载时有效,但在我尝试更新时无效。请注意,我对其他元素没有问题,只有这个自定义控件。

不知道为什么它不更新颜色。

标签: c#xamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


我注意到您在 中使用了CustomColor属性stacklayout,但是您在后台代码中添加了该IsAnonymousSubColor属性。CustomColor

我将您的CustomColor背景代码更改为以下格式(只需测试功能以更改颜色)。

  [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomGrid : Grid
    {

        public static readonly BindableProperty CustomColorProperty = BindableProperty.Create(nameof(CustomColor), typeof(Color), typeof(CustomGrid), propertyChanged: CustomColorChanged);

        public Color CustomColor
        {
            get { return (Color)GetValue(CustomColorProperty); }
            set { SetValue(CustomColorProperty, value); }
        }

        static void CustomColorChanged(object bindable, object oldValue, object newValue)
        {
            var x = bindable as CustomGrid;
            x.myLabelInMyXaml.BackgroundColor = x.CustomColor;

        }



        public CustomGrid()
        {
            InitializeComponent();
        }
    }

这里正在运行 GIF。

在此处输入图像描述


推荐阅读