简体   繁体   中英

Integrate Android View as custom control into Xamarin.Forms

I am using Xamarin Forms in my project.

Basically I want to integrate a custom control in my form. This component is given with an Android view (axml) and I cannot find a way to include it into my shared Xamarin Forms project.

I tried to create a custom renderer:

[assembly: ExportRenderer(typeof(RadialControl), typeof(RadialControlRenderer))]
namespace EA.Indemnisation.Renderers
{
    public class RadialControlRenderer : ViewRenderer
    {
        protected override void OnDraw(Android.Graphics.Canvas canvas)
        {
            base.OnDraw(canvas);

            SetContentView(Resource.Layout.Radial); // How can I include my view as component into the form?

            var bigRadialProgress = FindViewById<RadialProgressView>(Resource.Id.bigProgress);
            var smallRadialProgress = FindViewById<RadialProgressView>(Resource.Id.smallProgress);
            var tinyRadialProgress = FindViewById<RadialProgressView>(Resource.Id.tinyProgress);
        }
    }
}

That didn't help me because I am not able to get the Android view, interact with it and include it in my form.

I also looked at how to convert an Android.Views.View into a Xamarin.Forms.View but I cannot find a way to do that.

What is the way to integrate a custom control into a Xamarin Forms app?

You started in proper direction. Overrid OnElementChanged as there you are supposed to create your Android View.

Something like:

protected overrie void OnElementChanged(ElementChangedEventArgs<RadialControl> e)
{
   if (Control == null) {
      YourView yourView = new YourView(base.Context);
      SetNativeControl(yourView );
   }
   // update and/or bind properties & events
    ....
}

Furthermo to catch RadialControl's property changes you'd override OnElementPropertyChanged;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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