【发布时间】:2018-11-07 20:30:40
【问题描述】:
我的 Xamarin Forms 应用程序中有一个控件。现在我希望在它周围建立一个自定义进度条,比如边框倒计时。但要开始做事,我需要知道如何在我的元素(视图渲染器)周围正确定位它,就像一个完整的边框。我可以成功创建一个边框对象,但是对象的放置不正确。
我所做的是从我的元素中收集 X、Y、宽度和高度,但是当我为此添加断点时,它似乎没有给我任何有用和准确的数字。并且在我部署时显示,skiasharp 对象未按应有的方式对齐。
这就是我的工作:
public class SvgPathCountdown : SKCanvasView
{
public SvgPathCountdown()
{
this.InvalidateSurface();
}
private static void OnPropertyChanged(BindableObject bindable, object oldVal, object newVal)
{
var svgPath = bindable as SvgPathCountdown;
svgPath?.InvalidateSurface();
}
public static readonly BindableProperty XValueProperty =
BindableProperty.Create(nameof(XValue), typeof(double), typeof(SvgPathCountdown), 10.0, propertyChanged: OnPropertyChanged);
public double XValue
{
get { return (double)GetValue(XValueProperty); }
set { SetValue(XValueProperty, value); }
}
public static readonly BindableProperty YValueProperty =
BindableProperty.Create(nameof(YValue), typeof(double), typeof(SvgPathCountdown), 10.0, propertyChanged: OnPropertyChanged);
public double YValue
{
get { return (double)GetValue(YValueProperty); }
set { SetValue(YValueProperty, value); }
}
public static readonly BindableProperty WidthValueProperty =
BindableProperty.Create(nameof(WidthValue), typeof(double), typeof(SvgPathCountdown), 10.0, propertyChanged: OnPropertyChanged);
public double WidthValue
{
get { return (double)GetValue(WidthValueProperty); }
set { SetValue(WidthValueProperty, value); }
}
public static readonly BindableProperty HeightValueProperty =
BindableProperty.Create(nameof(HeightValue), typeof(double), typeof(SvgPathCountdown), 10.0, propertyChanged: OnPropertyChanged);
public double HeightValue
{
get { return (double)GetValue(HeightValueProperty); }
set { SetValue(HeightValueProperty, value); }
}
protected override void OnPaintSurface(SKPaintSurfaceEventArgs args)
{
SKImageInfo info = args.Info;
SKSurface surface = args.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear();
SKRect bounds;
var rect = SKRect.Create((float)XValue, (float)YValue, (float)WidthValue, (float)HeightValue);
var paint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Blue,
StrokeWidth = 10,
StrokeCap = SKStrokeCap.Round,
StrokeJoin = SKStrokeJoin.Round
};
// draw fill
canvas.DrawRect(rect, paint);
// change the brush (stroke with red)
paint.Style = SKPaintStyle.Stroke;
paint.Color = SKColors.Red;
// draw stroke
canvas.DrawRect(rect, paint);
}
还有 XAML:
<controls:CustomElement x:Name = "myControlElement" AbsoluteLayout.LayoutBounds="0.5, 0.2, 0.85, 0.2" AbsoluteLayout.LayoutFlags="All" />
<controls:SvgPathCountdown x:Name = "svgPath" />
这里的代码我设置了值:
svgPath.XValue = myControlElement.X;
svgPath.YValue = myControlElement.Y;
svgPath.WidthValue = myControlElement.AnchorX;
svgPath.HeightValue = myControlElement.HeightRequest;
当我使用后面代码中的设置值执行SKRect.Create 时,如何调整我的代码使其与元素匹配?也许我能以某种方式获得视图控件的 svg 路径?
【问题讨论】:
标签: c# xamarin xamarin.forms skiasharp