Behavior(行为)
Behavior
- 創建行為
- 使用Sytle來應用行為
- EffectBehavior
- 概述
- 創建行為
- 實現 BindableProperty
- 重寫OnAttachTo和OnDetachingFrom方法
- 添加和刪除行為功能
- 在平臺項目中各自實現效果類
- 使用Behavior
- 運行示例
創建行為
- 創建一個繼承自Behavior或Behavoir的自定義類,其中T是被將附加該行為的控件類型。
- 重寫OnAttachedTo方法以執行所需業務邏輯。
- 重寫OnDetachingFrom方法以執行所需的清理。
OnAttachedTo方法在行為附加到控件后立即觸發。
OnDetachingFrom方法在從控件中刪除行為時觸發。
使用Xamarin.Form行為
xaml:
C#:
var entry = new Entry { Placeholder = "Enter a System.Double" }; entry.Behaviors.Add (new NumericValidationBehavior ());使用Sytle來應用行為
應用行為
<ContentPage.Resources><Style x:Key="NumericValidationStyle" TargetType="Entry"><Style.Setters><Setter Property="behaviorsample:NumericValidationBehavior.AttachBehavior" Value="True"/></Style.Setters></Style></ContentPage.Resources> <Entry Placeholder="Enter a System.Double"Style="{StaticResource NumericValidationStyle}"/>官方Doc
EffectBehavior
使用Behavior可以向控件添加特定于平臺的效果、從隱藏代碼中省去冗余重復的效果處理代碼。
概述
EffectBehavior自定義類是可重用的Xmarin.Forms自定義行為,當行為附加到控件時,它會將Effect實例添加到控件,當行為與控件分離時,它將刪除Effect實例。
必須設置一下行為屬性才能使用該行為:
- Group - 平臺項目的自定義效果類特性ResolutionGroupName的屬性值。
- Name - 平臺項目的自定義效果類特性ExportEffect的屬性值。
創建行為
EffectBehavior自定義類派生自Behavior類,其中T是View。這意味著EffectBehavior自定義類可以附加到任何Xamarin.Forms控件。
實現 BindableProperty
在共享項目中,創建EffectBehavior自定義類,該自定義類定義了兩個 BindableProperty 實例,當行為附加到控件時,這些實例用于向控件添加Effect。
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 => GetValue(GroupProperty) as string;set => SetValue(GroupProperty,value);}public string Name{get => GetValue(NameProperty) as string;set => SetValue(NameProperty,value);}}當使用EffectBehavior時,應將Group屬性設置為特定于平臺效果類的ResolutionGroupName特性的值。此外,應將Name屬性設置為特定于平臺效果類的ExportEffect特性的值。
重寫OnAttachTo和OnDetachingFrom方法
protected override void OnAttachedTo(BindableObject bindable){base.OnAttachedTo(bindable);AddEffect(bindable as View);}protected override void OnDetachingFrom(BindableObject bindable){base.OnDetachingFrom(bindable);RemoveEffect(bindable as View);}添加和刪除行為功能
當行為附加到控件時,將Group和Name屬性中定義的Effect添加到控件,并在行為與控件分離時刪除Effefct。
Effect GetEffect(){if (!string.IsNullOrWhiteSpace(Group)&&!string.IsNullOrWhiteSpace(Name)){return Effect.Resolve($"{Group}.{Name}");}return null;}void AddEffect(View view){var effect = GetEffect();if (effect != null){view.Effects.Add(effect);}}void RemoveEffect(View view){var efffect = GetEffect();if (efffect !=null){view.Effects.Remove(GetEffect());}}GetEffect方法使用Effect.Resolve方法檢索Effect。通過串聯Group和Name屬性值來從平臺項目中定位檢索該效果。如果平臺項目不提供此效果,則Effect.Resolve方法將返回 NullEffect 實例。
在平臺項目中各自實現效果類
iOS
using System; using System.Diagnostics; using CoreGraphics; using EffectBehaviorSample.iOS; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS;[assembly:ResolutionGroupName("Xamarin")] [assembly:ExportEffect(typeof(LabelShadowEffect),"LabelShadowEffect")] namespace EffectBehaviorSample.iOS {public class LabelShadowEffect:PlatformEffect{public LabelShadowEffect(){}protected override void OnAttached(){try{Control.Layer.CornerRadius = 5;Control.Layer.ShadowColor = Color.Black.ToCGColor();Control.Layer.ShadowOffset = new CGSize(5,5);Control.Layer.ShadowOpacity = 1.0f;}catch (Exception ex){Debug.WriteLine("Cannot set property on attached control:",ex.Message);}}protected override void OnDetached(){throw new NotImplementedException();}} }Droid
using System; using System.Diagnostics; using EffectBehaviorSample.Droid; using Xamarin.Forms; using Xamarin.Forms.Platform.Android;[assembly:ResolutionGroupName("Xamarin")] [assembly:ExportEffect(typeof(LabelShadowEffect),"LabelShadowEffect")] namespace EffectBehaviorSample.Droid {public class LabelShadowEffect:PlatformEffect{public LabelShadowEffect(){}protected override void OnAttached(){try{var control = Control as Android.Widget.TextView;float radius = 5;float distanceX = 5;float distanceY = 10;Android.Graphics.Color color = Color.IndianRed.ToAndroid();control.SetShadowLayer(radius,distanceX,distanceY,color);}catch (Exception ex){Debug.WriteLine("Cannot set property on attached control. Error: ",ex.Message);}}protected override void OnDetached(){throw new NotImplementedException();}} }對于此示例,共享項目EffectBehavior的Group屬性應為"Xamarin",Name屬性應為"LabelShadowEffect",即可檢索到該特定于平臺的效果。
使用Behavior
EffectBehavior類可以附加到控件的Behaviors集合。
<StackLayout Margin="0,30,0,0"HorizontalOptions="Center" VerticalOptions="Center"><Label Text="Label Shadow Effect"><Label.Behaviors><effectbehaviorsample:EffectBehavior Group="Xamarin"Name="LabelShadowEffect"/></Label.Behaviors></Label></StackLayout>等到的C#
var label = new Label {Text = "Label Shadow Effect",... }; label.Behaviors.Add (new EffectBehavior {Group = "Xamarin",Name = "LabelShadowEffect" });行為的Group和Name屬性被設置為每個平臺項目的效果類的ResolutionGroupName和ExportEffect特性的值。
運行示例
iOS
Droid
==官方Doc==
總結
以上是生活随笔為你收集整理的Behavior(行为)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DW 简单效果代码整理
- 下一篇: Android中.9图片快捷的制作