A few changes so that the views can detect a designer

Related to #234
This commit is contained in:
Matthew Leibowitz 2017-04-25 23:34:05 -04:00
Родитель d87bdf5c78
Коммит 237e7bbf24
4 изменённых файлов: 48 добавлений и 8 удалений

Просмотреть файл

@ -13,6 +13,8 @@ namespace SkiaSharp.Views.Android
private SKImageInfo info;
private bool ignorePixelScaling;
private bool designMode;
public SKCanvasView(Context context)
: base(context)
{
@ -39,6 +41,11 @@ namespace SkiaSharp.Views.Android
private void Initialize()
{
designMode = !Extensions.IsValidEnvironment;
if (designMode)
return;
// create the initial info
info = new SKImageInfo(0, 0, SKColorType.Rgba8888, SKAlphaType.Premul);
}
@ -60,6 +67,9 @@ namespace SkiaSharp.Views.Android
{
base.OnDraw(canvas);
if (designMode)
return;
// create the bitmap data if we need it
if (bitmap == null || bitmap.Width != info.Width || bitmap.Height != info.Height)
{
@ -94,6 +104,9 @@ namespace SkiaSharp.Views.Android
private void UpdateCanvasSize(int w, int h)
{
if (designMode)
return;
if (IgnorePixelScaling)
{
var scale = Resources.DisplayMetrics.Density;

Просмотреть файл

@ -49,17 +49,20 @@ namespace SkiaSharp.Views.iOS
// created via designer
public override void AwakeFromNib()
{
designMode = ((IComponent)this).Site?.DesignMode == true;
Initialize();
}
private void Initialize()
{
designMode = ((IComponent)this).Site?.DesignMode == true || !Extensions.IsValidEnvironment;
if (designMode)
return;
drawable = new SKDrawable();
}
public SKSize CanvasSize => drawable.Info.Size;
public SKSize CanvasSize => drawable?.Info.Size ?? SKSize.Empty;
public bool IgnorePixelScaling
{
@ -75,7 +78,7 @@ namespace SkiaSharp.Views.iOS
{
base.Draw(rect);
if (designMode)
if (designMode || drawable == null)
return;
var ctx = UIGraphics.GetCurrentContext();
@ -109,7 +112,7 @@ namespace SkiaSharp.Views.iOS
{
base.Dispose(disposing);
drawable.Dispose();
drawable?.Dispose();
}
}
}

Просмотреть файл

@ -50,13 +50,16 @@ namespace SkiaSharp.Views.iOS
// created via designer
public override void AwakeFromNib()
{
designMode = ((IComponent)this).Site?.DesignMode == true;
Initialize();
}
private void Initialize()
{
designMode = ((IComponent)this).Site?.DesignMode == true || !Extensions.IsValidEnvironment;
if (designMode)
return;
// create the GL context
Context = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
DrawableColorFormat = GLKViewDrawableColorFormat.RGBA8888;

Просмотреть файл

@ -1,4 +1,5 @@
#if __ANDROID__
using System;
#if __ANDROID__
namespace SkiaSharp.Views.Android
#elif __TVOS__
namespace SkiaSharp.Views.tvOS
@ -14,6 +15,26 @@ namespace SkiaSharp.Views.Mac
{
public static class Extensions
{
private static readonly Lazy<bool> isValidEnvironment = new Lazy<bool>(() =>
{
try
{
// test an operation that requires the native library
SKPMColor.PreMultiply(SKColors.Black);
return true;
}
catch (DllNotFoundException)
{
// If we can't load the native library,
// we may be in some designer.
// We can make this assumption since any other member will fail
// at some point in the draw operation.
return false;
}
});
internal static bool IsValidEnvironment => isValidEnvironment.Value;
#if !WINDOWS_UWP
// System.Drawing.Point*