gpt4 book ai didi

c# - 将绑定(bind)值设置为 Xamarin Forms 中行为的参数

转载 作者:行者123 更新时间:2023-11-30 21:30:39 25 4
gpt4 key购买 nike

如何在 Xamarin Forms 中将绑定(bind)值设置为行为的参数?

当我这样做时,我得到:错误。找不到“InputValue”的属性、可绑定(bind)属性或事件,或者值和属性之间的类型不匹配。

XAML:

<Label Grid.Column="0" x:Name="player" FontSize="Small" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"
Text="{Binding FormattedName}" >
<Label.Behaviors>
<local:ColorTextLabelBehavior MaxInputValue="100" MinInputValue="0" InputValue="{Binding Happiness}" />
</Label.Behaviors>
</Label>

C#:

public class ColorTextLabelBehavior : Behavior<Label>
{
public static readonly BindableProperty MaxValue = BindableProperty.Create("MaxValue", typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 100);
public static readonly BindableProperty MinValue = BindableProperty.Create("MinValue", typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 0);
public static readonly BindableProperty ActualValue = BindableProperty.Create("ActualValue", typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 100);

public int MaxInputValue
{
get { return (int)GetValue(MaxValue); }
set { SetValue(MaxValue, value); }
}
public int MinInputValue
{
get { return (int)GetValue(MinValue); }
set { SetValue(MinValue, value); }
}
public int InputValue
{
get { return (int)GetValue(ActualValue); }
set { SetValue(ActualValue, value); }
}

void HandleTextChanged(object sender, PropertyChangedEventArgs e)
{
try
{
float percent = ((float)InputValue - (float)MinInputValue) / ((float)MaxInputValue - (float)MinInputValue);
double resultRed = Color.Red.R + percent * (Color.Green.R - Color.Red.R);
double resultGreen = Color.Red.G + percent * (Color.Green.G - Color.Red.G);
double resultBlue = Color.Red.B + percent * (Color.Green.B - Color.Red.B);
((Label)sender).TextColor = new Color(resultRed, resultGreen, resultBlue);
}
catch (Exception)
{

}
}

protected override void OnAttachedTo(BindableObject bindable)
{
bindable.PropertyChanged += HandleTextChanged;
}

protected override void OnDetachingFrom(BindableObject bindable)
{
bindable.PropertyChanged += HandleTextChanged;
}
}

如果我对“InputValue”的值进行硬编码,这是可行的,但我希望能够发送绑定(bind),以便它自动为文本着色。

请注意,如果重要的话,这是在 ListView 中。

请帮忙,我一直坚持这个,找不到任何答案。我也愿意接受其他实现方案。

谢谢!!

最佳答案

我认为准确的命名很重要,BindableProperty 必须在实际属性的名称末尾附加“Property”。来自docs :

The naming convention for bindable properties is that the bindable property identifier must match the property name specified in the Create method, with "Property" appended to it.

所以我相信你想要这样的东西:

public static readonly BindableProperty MaxInputValueProperty = BindableProperty.Create(nameof(MaxInputValue), typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 100);
public static readonly BindableProperty MinInputValueProperty = BindableProperty.Create(nameof(MinInputValue), typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 0);
public static readonly BindableProperty InputValueProperty = BindableProperty.Create(nameof(InputValue), typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 100);

public int MaxInputValue
{
get { return (int)GetValue(MaxValue); }
set { SetValue(MaxValue, value); }
}
public int MinInputValue
{
get { return (int)GetValue(MinValue); }
set { SetValue(MinValue, value); }
}
public int InputValue
{
get { return (int)GetValue(ActualValue); }
set { SetValue(ActualValue, value); }
}

关于c# - 将绑定(bind)值设置为 Xamarin Forms 中行为的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54086648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com