Xamarin.Forms
エフェクト
サーチ…
前書き
エフェクトは、プラットフォーム固有のカスタマイズを簡素化します。 Xamarin Forms Controlのプロパティを変更する必要がある場合、エフェクトを使用できます。 Xamarin Forms Controlのメソッドをオーバーライドする必要がある場合は、カスタムレンダラーを使用できます
エントリーコントロールにプラットフォーム固有の効果を追加する
- PCLファイル - >新規ソリューション - >マルチプラットフォームアプリケーション - > Xamarinフォーム - >フォームアプリケーションを使用して新しいXamarin Formsアプリケーションを作成します。プロジェクトの名前を
EffectsDemo - iOSのプロジェクトの下で、新しい追加
Effectを継承するクラスPlatformEffectクラスとメソッドよりも優先されますOnAttached、OnDetachedとOnElementPropertyChanged二つの属性に注目してくださいResolutionGroupNameとExportEffect、これらはPCL /共有プロジェクトからこの効果を消費するために必要とされます。
OnAttachedは、カスタマイズのロジックが入る方法ですOnDetachedはクリーンアップと登録OnDetachedが行われるメソッドですOnElementPropertyChangedは、異なる要素のプロパティ変更時にトリガされるメソッドです。適切なプロパティを識別するには、正確なプロパティの変更を確認し、ロジックを追加します。この例では、OnFocusはBlue、OutofFocusはRedusing System; using EffectsDemo.iOS; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ResolutionGroupName("xhackers")] [assembly: ExportEffect(typeof(FocusEffect), "FocusEffect")] namespace EffectsDemo.iOS { public class FocusEffect : PlatformEffect { public FocusEffect() { } UIColor backgroundColor; protected override void OnAttached() { try { Control.BackgroundColor = backgroundColor = UIColor.Red; } catch (Exception ex) { Console.WriteLine("Cannot set attacked property" + ex.Message); } } protected override void OnDetached() { throw new NotImplementedException(); } protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args) { base.OnElementPropertyChanged(args); try { if (args.PropertyName == "IsFocused") { if (Control.BackgroundColor == backgroundColor) { Control.BackgroundColor = UIColor.Blue; } else { Control.BackgroundColor = backgroundColor; } } } catch (Exception ex) { Console.WriteLine("Cannot set property " + ex.Message); } }}}
このエフェクトをアプリケーションで使用するには、
PCLプロジェクトで、FocusEffectから継承するRoutingEffectという名前の新しいクラスを作成します。これは、PCLがプラットフォーム固有のエフェクトのインプリメンテーションをインスタンス化するために不可欠です。以下のサンプルコード:using Xamarin.Forms; namespace EffectsDemo { public class FocusEffect : RoutingEffect { public FocusEffect() : base("xhackers.FocusEffect") { } } }XAMLの
Entryコントロールに効果を追加する<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:EffectsDemo" x:Class="EffectsDemo.EffectsDemoPage"> <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center"> <Label Text="Effects Demo" HorizontalOptions="StartAndExpand" VerticalOptions="Center" ></Label> <Entry Text="Controlled by effects" HorizontalOptions="FillAndExpand" VerticalOptions="Center"> <Entry.Effects> <local:FocusEffect> </local:FocusEffect> </Entry.Effects> </Entry> </StackLayout> </ContentPage>
エフェクトはiOSバージョンでのみ実装されていたため、 Entry背景色が変更されてもiOS Simulator実行され、 DroidプロジェクトでEffectが作成されなかったため、 Android Emulator何も起こりません
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow

