Xamarin.Forms でドラッグを実装しよう(UWP編)

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
Xamarin.Forms でドラッグを実装しよう(Xamarin.Forms on WinPhone編)
http://www.moonmile.net/blog/archives/7728

の続きです。

Xamarin.Forms は 2.0 からユニバーサルアプリ(UWP)を扱えるようになったハズなので、これを作ってみます。

Adding a Universal Windows Platform (UWP) App – Xamarin
https://developer.xamarin.com/guides/xamarin-forms/windows/getting-started/universal/

を参考にして、

– App.xaml.cs に Xamarin.Forms.Forms.Init(e); を追加
– MainPage.xaml に using:Xamarin.Forms.Platform.UWP を追加
– MainPage.xaml.cs に LoadApplication(new BoxDragXF.App()); を追加

すると動きます。

Android や iOS と同じようにドラッグさせたいので、Windows Phone 8.1 と同じように BoxExRenderer クラスを作ります。

[assembly: ExportRenderer(typeof(BoxViewEx), typeof(BoxExRenderer))]
namespace BoxDragXF.UWP
{
    class BoxExRenderer : Xamarin.Forms.Platform.UWP.BoxViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
        {
            base.OnElementChanged(e);
            this.ManipulationDelta += BoxExRenderer_ManipulationDelta1;
            this.ManipulationMode = Windows.UI.Xaml.Input.ManipulationModes.All;
        }

        private void BoxExRenderer_ManipulationDelta1(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e)
        {
            var el = this.Element as BoxViewEx;
            el.OnManipulationDelta(el, new BoxDragXF.ManipulationDeltaRoutedEventArgs(el, e.Delta.Translation.X, e.Delta.Translation.Y));
        }
    }
}

ほとんど、WinPhone 版と同じですね。
これが動くと、Windows IoT Core の UWP も同じように動くので、自作タブレットを作ったり、Surface を使った場合でも共通化できます。Android/iPhone/Surface の UI が Xamarin.Forms で一括で作ることができます。

動かしたときの動画はこちらです。

サンプルコード

https://github.com/moonmile/BoxDrag

にあります。

カテゴリー: 開発 パーマリンク