Xamarin.Forms でドラッグを実装しよう(のうりん編)
http://www.moonmile.net/blog/archives/7653
Xamarin.Forms でドラッグを実装しよう(Android編)
http://www.moonmile.net/blog/archives/7667
Xamarin.Forms でドラッグを実装しよう(Xamarin.Forms on Android編)
http://www.moonmile.net/blog/archives/7670
Xamarin.Forms でドラッグを実装しよう(Xamarin.Forms on iOS編)
http://www.moonmile.net/blog/archives/7723
の続きです。
最後は Windows Phone で動かします。
もともとの Xamarin.Forms のテンプレートを使っているので、Windows Phone 8.1 版になっていますが、おそらく Windows 10 用の UWP プロジェクトでも動くはずです。
Windows Phone のレンダラーを作る
BoxDragXF.WinPhone プロジェクトに BoxExRenderer クラスを追加します。Windows Phone/Mobile が一番簡単で(簡単なようにしたので)、そのまま ManipulationDelta メソッドを渡せば ok です。渡すときの引数は、疑似的なほうの ManipulationDeltaRoutedEventArgs オブジェクトなので、そこだけコンバートします。
[assembly: ExportRenderer(typeof(BoxViewEx), typeof(BoxExRenderer))] namespace BoxDragXF.WinPhone { class BoxExRenderer : Xamarin.Forms.Platform.WinPhone.BoxViewRenderer { protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e) { base.OnElementChanged(e); this.ManipulationDelta += BoxExRenderer_ManipulationDelta; } private void BoxExRenderer_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e) { var el = this.Element as BoxViewEx; el.OnManipulationDelta(el, new BoxDragXF.ManipulationDeltaRoutedEventArgs(el, e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y)); } } }
実行してみる
Hyper-V のエミュレーターで動かしてみます。これで、同じ Xamarin.Forms を使って、三機種(Android/iPhone/Windows Phone)で同じようにコントロールをドラッグできることがわかります。