From 54c29900c15afce47459f77875412494d1b4415b Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Thu, 15 Sep 2016 17:51:13 +0200 Subject: [PATCH] Added a view to UWP forms --- .../SKViewRenderer.cs | 78 +++++++++++++++++++ .../SkiaSharp.Views.Forms.UWP.csproj | 1 + .../SkiaSharp.Views.Forms.UWP.nuget.targets | 11 +++ .../SkiaSharp.Views.Forms.UWP/project.json | 5 +- 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SKViewRenderer.cs create mode 100644 source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.nuget.targets diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SKViewRenderer.cs b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SKViewRenderer.cs new file mode 100644 index 00000000..f94d98f7 --- /dev/null +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SKViewRenderer.cs @@ -0,0 +1,78 @@ +using System; +using Xamarin.Forms; +using Xamarin.Forms.Platform.UWP; + +using SKFormsView = SkiaSharp.Views.Forms.SKView; +using SKNativeView = SkiaSharp.Views.SKXamlCanvas; + +[assembly: ExportRenderer(typeof(SKFormsView), typeof(SkiaSharp.Views.Forms.SKViewRenderer))] + +namespace SkiaSharp.Views.Forms +{ + internal class SKViewRenderer : ViewRenderer + { + protected override void OnElementChanged(ElementChangedEventArgs e) + { + if (e.OldElement != null) + { + var oldController = (ISKViewController)e.OldElement; + + // unsubscribe from events + oldController.SurfaceInvalidated -= OnSurfaceInvalidated; + } + + if (e.NewElement != null) + { + var newController = (ISKViewController)e.NewElement; + + // create the native view + var view = new InternalView(newController); + SetNativeControl(view); + + // subscribe to events from the user + newController.SurfaceInvalidated += OnSurfaceInvalidated; + + // paint for the first time + Control.Invalidate(); + } + + base.OnElementChanged(e); + } + + protected override void Dispose(bool disposing) + { + // detach all events before disposing + var controller = (ISKViewController)Element; + if (controller != null) + { + controller.SurfaceInvalidated -= OnSurfaceInvalidated; + } + + base.Dispose(disposing); + } + + private void OnSurfaceInvalidated(object sender, EventArgs eventArgs) + { + // repaint the native control + Control.Invalidate(); + } + + private class InternalView : SKNativeView + { + private readonly ISKViewController controller; + + public InternalView(ISKViewController controller) + { + this.controller = controller; + } + + protected override void OnPaintSurface(SkiaSharp.Views.SKPaintSurfaceEventArgs e) + { + base.OnPaintSurface(e); + + // the control is being repainted, let the user know + controller.OnPaintSurface(new SKPaintSurfaceEventArgs(e.Surface, e.Info)); + } + } + } +} diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.csproj b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.csproj index 832de6c4..714c63ba 100644 --- a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.csproj +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.csproj @@ -108,6 +108,7 @@ + diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.nuget.targets b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.nuget.targets new file mode 100644 index 00000000..ae7688e0 --- /dev/null +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/SkiaSharp.Views.Forms.UWP.nuget.targets @@ -0,0 +1,11 @@ + + + + $(UserProfile)\.nuget\packages\ + + + + + + + \ No newline at end of file diff --git a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/project.json b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/project.json index 92d14561..4a08dba0 100644 --- a/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/project.json +++ b/source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.UWP/project.json @@ -1,6 +1,9 @@ { "dependencies": { - "Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0" + "Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0", + "SkiaSharp": "1.54.0.1", + "SkiaSharp.Views": "1.54.0.1-beta1", + "Xamarin.Forms": "2.3.0.49" }, "frameworks": { "uap10.0": {}