可重用 EffectBehaviorReusable EffectBehavior

下载示例 下载示例Download Sample Download the sample

行为是一种非常有用的方法,用于向控件添加效果、从代码隐藏文件中删除冗余重复的效果处理代码。本文演示如何创建 Xamarin.Forms 行为并将其用于向控件添加效果。Behaviors are a useful approach for adding an effect to a control, removing boiler-plate effect handling code from code-behind files. This article demonstrates creating and consuming a Xamarin.Forms behavior to add an effect to a control.

概述Overview

EffectBehavior 类是可重用的 Xamarin.Forms 自定义行为,当行为附加到控件时,它将 Effect 实例添加到控件,当行为与控件分离时,它将删除 Effect 实例。The EffectBehavior class is a reusable Xamarin.Forms custom behavior that adds an Effect instance to a control when the behavior is attached to the control, and removes the Effect instance when the behavior is detached from the control.

必须设置以下行为属性才能使用该行为:The following behavior properties must be set to use the behavior:

有关效果的更多信息,请参阅效果For more information about effects, see Effects.

备注

EffectBehavior 是一个自定义类,它可以位于效果行为示例中,但并不属于 Xamarin.Forms。The EffectBehavior is a custom class that can be located in the Effect Behavior sample, and is not part of Xamarin.Forms.

创建行为Creating the Behavior

EffectBehavior 类派生自 Behavior<T> 类,其中 TViewThe EffectBehavior class derives from the Behavior<T> class, where T is a View. 这意味着,EffectBehavior 类可以附加到任何 Xamarin.Forms 控件。This means that the EffectBehavior class can be attached to any Xamarin.Forms control.

实现可绑定属性Implementing Bindable Properties

EffectBehavior 类定义了两个 BindableProperty 实例,当行为附加到控件时,这些实例用于向控件添加 EffectThe EffectBehavior class defines two BindableProperty instances, which are used to add an Effect to a control when the behavior is attached to the control. 以下代码示例中显示了这些属性:These properties are shown in the following code example:

public class EffectBehavior : Behavior<View>
{
  public static readonly BindableProperty GroupProperty =
    BindableProperty.Create ("Group", typeof(string), typeof(EffectBehavior), null);
  public static readonly BindableProperty NameProperty =
    BindableProperty.Create ("Name", typeof(string), typeof(EffectBehavior), null);

  public string Group {
    get { return (string)GetValue (GroupProperty); }
    set { SetValue (GroupProperty, value); }
  }

  public string Name {
    get { return(string)GetValue (NameProperty); }
    set { SetValue (NameProperty, value); }
  }
  ...
}

当使用 EffectBehavior 时,应将 Group 属性设置为效果的 ResolutionGroupName 特性的值。When the EffectBehavior is consumed, the Group property should be set to the value of the ResolutionGroupName attribute for the effect. 此外,应将 Name 属性设置为效果类的 ExportEffect 特性的值。In addition, the Name property should be set to the value of the ExportEffect attribute for the effect class.

实现替代Implementing the Overrides

EffectBehavior 类替代 Behavior<T> 类的 OnAttachedToOnDetachingFrom 方法,如以下代码示例所示:The EffectBehavior class overrides the OnAttachedTo and OnDetachingFrom methods of the Behavior<T> class, as shown in the following code example:

public class EffectBehavior : Behavior<View>
{
  ...
  protected override void OnAttachedTo (BindableObject bindable)
  {
    base.OnAttachedTo (bindable);
    AddEffect (bindable as View);
  }

  protected override void OnDetachingFrom (BindableObject bindable)
  {
    RemoveEffect (bindable as View);
    base.OnDetachingFrom (bindable);
  }
  ...
}

OnAttachedTo 方法通过调用 AddEffect 方法执行设置,并将附加的控件作为参数传递。The OnAttachedTo method performs setup by calling the AddEffect method, passing in the attached control as a parameter. OnDetachingFrom 方法通过调用 RemoveEffect 方法执行清理,并将附加的控件作为参数传递。The OnDetachingFrom method performs cleanup by calling the RemoveEffect method, passing in the attached control as a parameter.

实现行为功能Implementing the Behavior Functionality

此行为的目的是,当行为附加到控件时,将 GroupName 属性中定义的 Effect 添加到控件,并在行为与控件分离时删除 EffectThe purpose of the behavior is to add the Effect defined in the Group and Name properties to a control when the behavior is attached to the control, and remove the Effect when the behavior is detached from the control. 核心行为功能如以下代码示例所示:The core behavior functionality is shown in the following code example:

public class EffectBehavior : Behavior<View>
{
  ...
  void AddEffect (View view)
  {
    var effect = GetEffect ();
    if (effect != null) {
      view.Effects.Add (GetEffect ());
    }
  }

  void RemoveEffect (View view)
  {
    var effect = GetEffect ();
    if (effect != null) {
      view.Effects.Remove (GetEffect ());
    }
  }

  Effect GetEffect ()
  {
    if (!string.IsNullOrWhiteSpace (Group) && !string.IsNullOrWhiteSpace (Name)) {
      return Effect.Resolve (string.Format ("{0}.{1}", Group, Name));
    }
    return null;
  }
}

执行 AddEffect 方法以响应要附加到控件的 EffectBehavior,并且它将以参数形式接收所附加的控件。The AddEffect method is executed in response to the EffectBehavior being attached to a control, and it receives the attached control as a parameter. 然后,该方法将检索到的效果添加到控件的 Effects 集合。The method then adds the retrieved effect to the control's Effects collection. 执行 RemoveEffect 方法以相应要与控件拆离的 EffectBehavior,并且它将以参数形式接收所附加的控件。The RemoveEffect method is executed in response to the EffectBehavior being detached from a control, and it receives the attached control as a parameter. 然后该方法从控件的 Effects 集合中移除效果。The method then removes the effect from the control's Effects collection.

GetEffect 方法使用 Effect.Resolve 方法检索 EffectThe GetEffect method uses the Effect.Resolve method to retrieve the Effect. 通过串联 GroupName 属性值来定位该效果。The effect is located through a concatenation of the Group and Name property values. 如果平台不提供此效果,则 Effect.Resolve 方法将返回一个非 null 值。If a platform doesn't provide the effect, the Effect.Resolve method will return a non-null value.

使用行为Consuming the Behavior

EffectBehavior 类可以附加到控件的 Behaviors 集合,如以下 XAML 代码示例所示:The EffectBehavior class can be attached to the Behaviors collection of a control, as demonstrated in the following XAML code example:

<Label Text="Label Shadow Effect" ...>
  <Label.Behaviors>
    <local:EffectBehavior Group="Xamarin" Name="LabelShadowEffect" />
  </Label.Behaviors>
</Label>

以下代码示例显示相应的 C# 代码:The equivalent C# code is shown in the following code example:

var label = new Label {
  Text = "Label Shadow Effect",
  ...
};
label.Behaviors.Add (new EffectBehavior {
  Group = "Xamarin",
  Name = "LabelShadowEffect"
});

行为的 GroupName 属性被设置为每个平台特定项目的效果类的 ResolutionGroupNameExportEffect 特性的值。The Group and Name properties of the behavior are set to the values of the ResolutionGroupName and ExportEffect attributes for the effect class in each platform-specific project.

在运行时,当行为附加到 Label 控件时,Xamarin.LabelShadowEffect 将添加到控件的 Effects 集合中。At runtime, when the behavior is attached to the Label control, the Xamarin.LabelShadowEffect will be added to the control's Effects collection. 这就使阴影被添加到 Label 控件显示的文本中,如以下屏幕截图所示:This results in a shadow being added to the text displayed by the Label control, as shown in the following screenshots:

使用这种行为从控件中添加和删除效果的优点是,可以从代码隐藏文件中删除冗余重复的效果处理代码。The advantage of using this behavior to add and remove effects from controls is that boiler-plate effect-handling code can be removed from code-behind files.

总结Summary

本文演示了如何使用行为向控件添加效果。This article demonstrated using a behavior to add an effect to a control. EffectBehavior 类是可重用的 Xamarin.Forms 自定义行为,当行为附加到控件时,它将 Effect 实例添加到控件,当行为与控件分离时,它将删除 Effect 实例。The EffectBehavior class is a reusable Xamarin.Forms custom behavior that adds an Effect instance to a control when the behavior is attached to the control, and removes the Effect instance when the behavior is detached from the control.