日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Behavior(行为)

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Behavior(行为) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Behavior

  • 創建行為
  • 使用Sytle來應用行為
  • EffectBehavior
    • 概述
    • 創建行為
    • 實現 BindableProperty
    • 重寫OnAttachTo和OnDetachingFrom方法
    • 添加和刪除行為功能
    • 在平臺項目中各自實現效果類
    • 使用Behavior
    • 運行示例

創建行為

  • 創建一個繼承自Behavior或Behavoir的自定義類,其中T是被將附加該行為的控件類型。
  • 重寫OnAttachedTo方法以執行所需業務邏輯。
  • 重寫OnDetachingFrom方法以執行所需的清理。
public class NumericValidationBehavior:Behavior<Entry>{protected override void OnAttachedTo(Entry bindable){base.OnAttachedTo(bindable);bindable.TextChanged += OnEntryTextChanged;}protected override void OnDetachingFrom(Entry bindable){base.OnDetachingFrom(bindable);bindable.TextChanged -= OnEntryTextChanged;}void OnEntryTextChanged(object sender,TextChangedEventArgs args){double result;bool isValid = double.TryParse(args.NewTextValue, out result);(sender as Entry).TextColor = isValid ?Color.Default:Color.Red;}}

OnAttachedTo方法在行為附加到控件后立即觸發。
OnDetachingFrom方法在從控件中刪除行為時觸發。
使用Xamarin.Form行為
xaml:

<Entry Placeholder="Enter a System.Double"><Entry.Behaviors><behaviorsample:NumericValidationBehavior/></Entry.Behaviors></Entry>

C#:

var entry = new Entry { Placeholder = "Enter a System.Double" }; entry.Behaviors.Add (new NumericValidationBehavior ());

使用Sytle來應用行為

  • 將附加屬性添加到自定義的行為類中,以控制從控件中添加和刪除行為。確保附加屬性注冊propertyChanged委托,該委托將在屬性值更改時執行。
  • 為附加屬性創建static的getter和setter。
  • 在propertyChanged委托中實現邏輯以添加和刪除行為。
  • #region 使用Style來定義Behavior,通過附加屬性來實現public static readonly BindableProperty AttachBehaviorProperty = BindableProperty.CreateAttached("AttachBehavior",typeof(bool),typeof(NumericValidationBehavior),false,propertyChanged:OnAttachBehaviorChanged);public static bool GetAttachBehavior(BindableObject view)=> (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 is null){return;}bool attachBehavior = (bool)newValue;if (attachBehavior){entry.Behaviors.Add(new NumericValidationBehavior());}else{var toRemove = entry.Behaviors.FirstOrDefault(b => b is NumericValidationBehavior);if (toRemove != null){entry.Behaviors.Remove(toRemove);}}}#endregion

    應用行為

    <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(行为)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。