UWP中是否存在WPF的Geometry.Data.GetFlattenedGeometry的等价物?

时间:2017-10-09 13:34:33

标签: c# uwp

有没有办法在UWP应用中的Point几何图形中找到 x%Path?在WPF中,可以使用:

var path = new Path { Data = Geometry.Parse(svgLikePathString) };
var flattened = path.Data.GetFlattenedGeometry();

// e.g. x = 0.5 for 50% along the path
flattened.GetPointAtFractionLength(x, out Point point, out Point tangent);

上下文我将基于WPF的应用程序转换为UWP,我可以在同一个点阵列中显示不同的路径,有点像https://bl.ocks.org/mbostock/4342190。在UWP中,我可以使用this approach替换Geometry.Parse(string),但path.Data.GetFlattenedGeometry()上不再存在GetPointAtFractionLength()Geometry

1 个答案:

答案 0 :(得分:0)

由于某种原因,UWP上的路径检查功能已被删除。我已经设法使用SkiaSharp拼凑了一个解决方案(我也考虑过Win2D,但SkiaSharp支持更多平台)。

在我生成代码的路径中,我之前使用GetFlattenedGeometry我执行以下操作:

// using SkiaSharp;

var path = SKPath.ParseSvgPathData(mySvgLikePathString);
var measure = SKPathMeasure(path);    
var pathLength = measure.Length;

// get the position and tangent half way along the path
measure.GetPositionAndTangent(0.5 * pathLength, out SKPoint pos, out SKPoint tangent);

// do some magic to generate the SVG-like path in here and return the string representation
return GenerateSuperSpecialPath(path, pos, tangent); 

获得路径字符串后,我可以将其放入ViewModel并将其绑定在模板中:

<Path Data={Binding MyPathString, Converter={StaticResource StringToGeometryConverter}} />

转换器如下所示:

public class StringToGeometryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var pathStr = "<Geometry xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" + (string)value + "</Geometry>";
        return (Geometry)XamlReader.Load(pathStr);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new InvalidOperationException("Cannot convert from Geometry to string");
    }
}

可能不是性能最佳的解决方案,但它有效,而且在绘制路径时我没有注意到任何严重的延迟。