【问题标题】:Set binding value as a parameter on a behavior in Xamarin Forms将绑定值设置为 Xamarin Forms 中行为的参数
【发布时间】:2019-06-02 20:11:53
【问题描述】:

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

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

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”硬编码一个值,这是可行的,但我希望能够发送一个绑定,以便它自动为文本着色。

如果重要的话,请注意这是在列表视图中。

请帮忙,我一直卡在这个问题上,找不到任何答案。 我也愿意接受其他替代方案来实现这一点。

谢谢!!

【问题讨论】:

    标签: c# xamarin binding label behavior


    【解决方案1】:

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

    可绑定属性的命名约定是可绑定属性标识符必须与 Create 方法中指定的属性名称匹配,并附加“Property”。

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

    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); }
    }
    

    【讨论】:

    • 我将代码重构为如下所示,并且不再出现错误,但是 InputValue 始终是默认值 100,而不是我发送的绑定 - public static BindableProperty InputValueProperty = BindableProperty.Create(nameof( InputValue), typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 100);公共 int InputValue { get { return (int)GetValue(InputValueProperty); } set { SetValue(InputValueProperty, value); } }
    • 所以问一个新问题,为什么绑定不起作用。您还需要显示 XAML 代码。
    • 快速浏览会发现一个可能的问题。您可能需要在OnAttachedTo 和OnDetachingFrom 方法中调用base,例如:base.onAttachedTo(bindable),但您也是从Behavior&lt;Label&gt; 继承的,但您说这是一个Entry。如果是这样,您需要从 Behavior 继承。如果这没有帮助,请发布一个新问题。见:docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
    • 另外,既然这个答案解决了最初的错误,你能接受它作为答案吗?谢谢。
    • 感谢您帮助我解决此问题。我在这里为另一个问题创建了另一个问题 - stackoverflow.com/questions/54102145/…
    【解决方案2】:

    这个问题很简单

    如果您检查您的BindableProperty 是否有ActualValue:

     public int InputValue
    {
        get { return (int)GetValue(ActualValue); }
        set { SetValue(ActualValue, value); }
    }
    
    public static readonly BindableProperty ActualValue = BindableProperty.Create("ActualValue", typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 100);
    

    您将属性名称设置为ActualValue,而它应该是关联属性的名称而不是 Binding 字段,因此您的属性应该是这样的:

    public static readonly BindableProperty ActualValue = BindableProperty.Create(nameof(InputValue), typeof(int), typeof(ColorTextLabelBehavior), defaultValue: 100);
    
    public int InputValue
    {
        get { return (int)GetValue(ActualValue); }
        set { SetValue(ActualValue, value); }
    }
    

    对其他两个属性也进行相同的修正。

    还要注意nameof(InputValue) 与“InputValue”相同,使用nameof() 是标准的做法

    【讨论】:

    • 我试过这样做,我仍然得到同样的错误,它在运行时立即失败。似乎该行为不允许绑定到其中一个参数。
    猜你喜欢
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2017-03-15
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多