我创建了一个自定义控件,我想在点击后改变颜色。我可以成功地加载它,但是一旦我运行我的函数并想改变颜色,就什么都没有发生。当我尝试用常规元素(按钮、标签)来做时,它可以工作。
自定义控件(就是一个网格,里面有一个简单的标签)。
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 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>
ViewModel。
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>
但是自定义控件的背景颜色没有改变。当我最初加载时,它可以工作,但当我试图更新它时就不行了。请注意,我对其他元素没有问题,只有这个自定义控件。
不知道为什么它不更新颜色。
我注意到你使用了 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。
这就是问题所在。
x.myLabelInMyXaml.BackgroundColor = x.CustomColor;
把它改成
x.myLabelInMyXaml.BackgroundColor = (Color) newValue;
我想你可能是绑定上下文的问题。
在没有看到所有上下文的代码之前,很难说。
首先,你为什么把一个属性叫做视图模型?这个名字其实并不重要,但它很容易让人混淆,因为一个属性不是视图模型,如果没有看到这个属性所在的类,很难说这是怎么回事。
我怀疑问题出在这里。
<controls:CustomGrid Item ="{Binding .}"
CustomColor = "{Binding BindingContext.CustomColorViewModel, Source={x:Reference ParentView}}" />
我认为你对CustomColor的绑定代码应该是(假设你的父视图真的被命名为 "CustomColor")。ParentView
):
CustomColor = "{Binding Source={x:Reference ParentView}, Path=BindingContext.CustomColorViewModel}"
但这也是基于一些重要的代码信息的缺失, 但希望至少能让你朝着正确的方向前进, 即使不是解决方案。