Xamarin Forms如何将行为添加到自定义控件

时间:2019-07-11 10:07:39

标签: xamarin.forms custom-controls behavior

我创建了一个自定义控件,它是一个ContentView和一个Label和一个Entry

自定义控件的xaml如下所示:

<Label Text="{Binding Source={x:Reference ValidationControl}, Path=Caption}"/>
<Entry Text="{Binding Source={x:Reference ValidationControl}, Path=Value, Mode=TwoWay}" />

自定义控件后面的代码如下:

public static readonly BindableProperty CaptionProperty = BindableProperty.Create(
    nameof(Caption), typeof(string), typeof(ValidationEntry), default(string));

public string Caption
{
    get => (string)GetValue(CaptionProperty);
    set => SetValue(CaptionProperty, value);
}

public static readonly BindableProperty ValueProperty = BindableProperty.Create(
    nameof(Value), typeof(string), typeof(ValidationEntry), default(string));

public string Value
{
    get => (string)GetValue(ValueProperty);
    set => SetValue(ValueProperty, value);
}

我通过以下方式使用自定义控件

<controls:ValidationEntry Caption=”Name:” Value="{Binding FullName, Mode=TwoWay}" />

我的问题是如何向自定义控件添加行为?
我想将它们添加到我使用控件的位置。即

<controls:ValidationEntry Caption="Name:"
                          Value="{Binding FullName, Mode=TwoWay}">
    <controls:ValidationEntry.EntryBehaviors>
        <behaviors:EntryLengthValidatorBehavior IgnoreSpaces="True"/>
    </controls:ValidationEntry.EntryBehaviors>
</controls:ValidationEntry>

1 个答案:

答案 0 :(得分:0)

您可以直接创建一个行为,我在自定义条目中添加了一个NumericValidationBehavior来检查数据是否为double。如果数据的类型不是double,则文本的颜色将设置为红色。

enter image description here

这是xaml代码。

<StackLayout>
<local:MyEntry   local:NumericValidationBehavior.AttachBehavior="true">


</local:MyEntry>
</StackLayout>

这里是NumericValidationBehavior.cs

     public static class NumericValidationBehavior
{
    public static readonly BindableProperty AttachBehaviorProperty =
        BindableProperty.CreateAttached(
            "AttachBehavior",
            typeof(bool),
            typeof(NumericValidationBehavior),
            false,
            propertyChanged: OnAttachBehaviorChanged);

    public static bool GetAttachBehavior(BindableObject view)
    {
        return (bool)view.GetValue(AttachBehaviorProperty);
    }

    public static void SetAttachBehavior(BindableObject view, bool value)
    {
        view.SetValue(AttachBehaviorProperty, value);
    }

    static void OnAttachBehaviorChanged(BindableObject view, object oldValue, object newValue)
    {
        var entry = view as Entry;
        if (entry == null)
        {
            return;
        }

        bool attachBehavior = (bool)newValue;
        if (attachBehavior)
        {
            entry.TextChanged += OnEntryTextChanged;
        }
        else
        {
            entry.TextChanged -= OnEntryTextChanged;
        }
    }

    static void OnEntryTextChanged(object sender, TextChangedEventArgs args)
    {
        double result;
        bool isValid = double.TryParse(args.NewTextValue, out result);
        ((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
    }
}

更新

我使用ContentView创建自定义视图

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="BeHavDemo.MyView">
 <ContentView.Content>
     <StackLayout>
        <Label Text="xxxx"/>
        <Entry Text="eeeee" />
      </StackLayout>
  </ContentView.Content>
</ContentView>

然后我创建一个行为。

    public  class MyBeha : Behavior<MyView>
{
    protected override void OnAttachedTo(BindableObject view)
    {
        base.OnAttachedTo(view);

        var myview=view as MyView;
        StackLayout stackLayout = (StackLayout)myview.Content;

        Label label = (Label)stackLayout.Children[0];
       Entry entry=(Entry) stackLayout.Children[1];

    }
}
相关问题