简体   繁体   English

将Android View作为自定义控件集成到Xamarin.Forms中

[英]Integrate Android View as custom control into Xamarin.Forms

I am using Xamarin Forms in my project. 我在项目中使用Xamarin Forms。

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. 该组件具有Android视图(axml),我找不到将其包含到共享Xamarin Forms项目中的方法。

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. 那对我没有帮助,因为我无法获取Android视图,无法与其交互并将其包含在表单中。

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. 我还研究了如何将Android.Views.View转换为Xamarin.Forms.View但我找不到解决方法。

What is the way to integrate a custom control into a Xamarin Forms app? 将自定义控件集成到Xamarin Forms应用程序的方法是什么?

You started in proper direction. 您开始朝正确的方向前进。 Overrid OnElementChanged as there you are supposed to create your Android View. 覆盖OnElementChanged,因为您应该在那里创建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; 进一步捕获RadialControl的属性更改,您将覆盖OnElementPropertyChanged;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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