简体   繁体   English

将SkiaSharp的PaintSurface与Xamarin形式和棱镜绑定

[英]Binding SkiaSharp's PaintSurface with Xamarin Forms and Prism

I'm trying to get SkiaSharp working with Xamarin Forms and Prism. 我正在尝试让SkiaSharp与Xamarin Forms和Prism一起使用。 I have it working with the following page behind code 我在下面的代码背后使用它

public partial class RoomLayoutPage : ContentPage 
{ 

SKCanvasView canvasView;

public RoomLayoutPage()
{
  InitializeComponent();

  canvasView = new SKCanvasView();
  canvasView.PaintSurface += OnCanvasViewPaintSurface;
  Content = canvasView;
}


private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
  SKSurface surface = e.Surface;
  SKCanvas canvas = surface.Canvas;

  canvas.Clear();

  SKPaint blackPaint = new SKPaint
  {
    Color = SKColors.Black,
  };

  canvas.DrawRect(0, 0, 100, 100, blackPaint);
}
}

but now I want to move this to my Prism ViewModel. 但现在我想将其移至我的Prism ViewModel。 If I move it then Content is not valid 如果我将其移动,则内容无效

  1. How do I reference a xaml element from a ViewModel? 如何从ViewModel引用xaml元素? I'd prefer not to do it this way because my ViewModel is then coupled to the view. 我不想这样做,因为我的ViewModel然后耦合到了视图。
  2. (Prefered way) If I put an SKCanvasView on my page (首选方式)如果我将SKCanvasView放在页面上

I can bind to the event with the EventToCommandBehaviour 我可以使用EventToCommandBehaviour绑定到事件

    <forms:SKCanvasView>
  <forms:SKCanvasView.Behaviors>
    <behaviors:EventToCommandBehavior Command="{Binding OnCanvasViewPaintSurface}" EventName="PaintSurface"/>
  </forms:SKCanvasView.Behaviors>
</forms:SKCanvasView>

But I'm not sure how to bind the SKPaintSurfaceEventArgs for 但是我不确定如何绑定SKPaintSurfaceEventArgs

OnCanvasViewPaintSurface = new DelegateCommand<SKPaintSurfaceEventArgs>(OnCanvasViewPaintAction);

I'm assuming it's one of the EventArgs options from here https://prismlibrary.github.io/docs/xamarin-forms/EventToCommandBehavior.html ? 我假设它是来自这里https://prismlibrary.github.io/docs/xamarin-forms/EventToCommandBehavior.html的EventArgs选项之一?

Thank you 谢谢

So using a value converter solves it.. 因此,使用值转换器即可解决该问题。

public class SkiaEventArgsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
  var sKPaintSurfaceEventArgs = value as SKPaintSurfaceEventArgs;
  if (sKPaintSurfaceEventArgs == null)
  {
    throw new ArgumentException("Expected value to be of type SKPaintSurfaceEventArgs", nameof(value));
  }

  return sKPaintSurfaceEventArgs;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
  throw new NotImplementedException();
}
}

Not entirely sure why it couldn't convert it automagically but it works 不能完全确定为什么它不能自动转换但可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM