Sankarann
Sankarann

Reputation: 2665

Is there anything in Xamarin forms which is equivalent to "Path" in WPF?

I'm looking after to embed a vector path data in Image control. WPF has a Control "Path" which takes data and render the image. Is there anything in Xamarin forms to do this?

Upvotes: 1

Views: 218

Answers (1)

Mark Feldman
Mark Feldman

Reputation: 16119

In a word, no. Xamarin Forms and WPF are fundamentally different at the rendering layer.

WPF is tightly integrated into Direct3D via milcore, which enabled the developers to re-build all windows GUI elements from the ground up (buttons, checkboxes, text blocks etc). This, coupled with templating, is what makes the "P" in WPF so incredibly flexible.

Unfortunately this comes with a huge price: performance. There's no way hand-held devices would be able to provide the bandwidth needed to support such an engine, so Xamarin Forms had to instead provide classes that were implemented using each respective operating system's native GUI controls; in this respect it's closer to WinForms. This meant that the base set of Xamarin Forms controls had to be whatever controls were common to all supported build types i.e. iOS, Android, UWP etc. The bottom line is that anything requiring the kind of fine granularity of Path will probably run too slow on these platforms if implemented in Forms and you'd just have to wind up implementing them natively anyway.

If you want to go down this path yourself then it can certainly be done by creating a custom control in your PCL project based on View (say) and then providing a native renderer for it in each of your targets, i.e. something like this:

// Android renderer for your custom control
[assembly: ExportRenderer(typeof(YourCustomControl), typeof(YourCustomControlRenderer))]
namespace Your.Project.Namespace
{
    public class YourCustomControlRenderer : VisualElementRenderer<YourCustomControl>
    {
        protected override void OnDraw(Canvas canvas)
        {
            ... etc ...
            canvas.DrawRect(new RectF(drawRect), bkgPaint);
            ... etc ...
        }
    }
}

Or, as SushiuHangover points out, you can use a 3rd-party lib which will basically just be doing this anyway.

Upvotes: 3

Related Questions