我正在尝试使Skiasharp与Xamarin形式和棱镜一起工作。我将其与代码背后的以下页面一起工作
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);
}
}
,但是现在我想将其移至我的棱镜ViewModel。如果我移动它,则内容无效
- 如何从ViewModel引用XAML元素?我宁愿不这样做,因为然后将我的ViewModel耦合到视图。
(优先方式(如果我在页面上放置了一个skcanvasview
我可以使用EventTocommandBehaviour
绑定到事件 <forms:SKCanvasView>
<forms:SKCanvasView.Behaviors>
<behaviors:EventToCommandBehavior Command="{Binding OnCanvasViewPaintSurface}" EventName="PaintSurface"/>
</forms:SKCanvasView.Behaviors>
</forms:SKCanvasView>
但我不确定如何绑定
的skpaintsurfaceeventargsOnCanvasViewPaintSurface = new DelegateCommand<SKPaintSurfaceEventArgs>(OnCanvasViewPaintAction);
我假设这是此处的EventArgs选项之一https://prismlibrary.github.io/docs/xamarin-forms/eventtocommandbehavior.html?
谢谢
,因此使用值转换器求解它..
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();
}
}
不完全确定为什么它不能自动转换,但起作用