Behavior(行为)
Behavior
- 創(chuàng)建行為
- 使用Sytle來應(yīng)用行為
- EffectBehavior
- 概述
- 創(chuàng)建行為
- 實(shí)現(xiàn) BindableProperty
- 重寫OnAttachTo和OnDetachingFrom方法
- 添加和刪除行為功能
- 在平臺(tái)項(xiàng)目中各自實(shí)現(xiàn)效果類
- 使用Behavior
- 運(yùn)行示例
創(chuàng)建行為
- 創(chuàng)建一個(gè)繼承自Behavior或Behavoir的自定義類,其中T是被將附加該行為的控件類型。
- 重寫OnAttachedTo方法以執(zhí)行所需業(yè)務(wù)邏輯。
- 重寫OnDetachingFrom方法以執(zhí)行所需的清理。
OnAttachedTo方法在行為附加到控件后立即觸發(fā)。
OnDetachingFrom方法在從控件中刪除行為時(shí)觸發(fā)。
使用Xamarin.Form行為
xaml:
C#:
var entry = new Entry { Placeholder = "Enter a System.Double" }; entry.Behaviors.Add (new NumericValidationBehavior ());使用Sytle來應(yīng)用行為
應(yīng)用行為
<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可以向控件添加特定于平臺(tái)的效果、從隱藏代碼中省去冗余重復(fù)的效果處理代碼。
概述
EffectBehavior自定義類是可重用的Xmarin.Forms自定義行為,當(dāng)行為附加到控件時(shí),它會(huì)將Effect實(shí)例添加到控件,當(dāng)行為與控件分離時(shí),它將刪除Effect實(shí)例。
必須設(shè)置一下行為屬性才能使用該行為:
- Group - 平臺(tái)項(xiàng)目的自定義效果類特性ResolutionGroupName的屬性值。
- Name - 平臺(tái)項(xiàng)目的自定義效果類特性ExportEffect的屬性值。
創(chuàng)建行為
EffectBehavior自定義類派生自Behavior類,其中T是View。這意味著EffectBehavior自定義類可以附加到任何Xamarin.Forms控件。
實(shí)現(xiàn) BindableProperty
在共享項(xiàng)目中,創(chuàng)建EffectBehavior自定義類,該自定義類定義了兩個(gè) BindableProperty 實(shí)例,當(dāng)行為附加到控件時(shí),這些實(shí)例用于向控件添加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);}}當(dāng)使用EffectBehavior時(shí),應(yīng)將Group屬性設(shè)置為特定于平臺(tái)效果類的ResolutionGroupName特性的值。此外,應(yīng)將Name屬性設(shè)置為特定于平臺(tái)效果類的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);}添加和刪除行為功能
當(dāng)行為附加到控件時(shí),將Group和Name屬性中定義的Effect添加到控件,并在行為與控件分離時(shí)刪除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。通過串聯(lián)Group和Name屬性值來從平臺(tái)項(xiàng)目中定位檢索該效果。如果平臺(tái)項(xiàng)目不提供此效果,則Effect.Resolve方法將返回 NullEffect 實(shí)例。
在平臺(tái)項(xiàng)目中各自實(shí)現(xiàn)效果類
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();}} }對(duì)于此示例,共享項(xiàng)目EffectBehavior的Group屬性應(yīng)為"Xamarin",Name屬性應(yīng)為"LabelShadowEffect",即可檢索到該特定于平臺(tái)的效果。
使用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屬性被設(shè)置為每個(gè)平臺(tái)項(xiàng)目的效果類的ResolutionGroupName和ExportEffect特性的值。
運(yùn)行示例
iOS
Droid
==官方Doc==
總結(jié)
以上是生活随笔為你收集整理的Behavior(行为)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DW 简单效果代码整理
- 下一篇: 在线PNG转ICO