2014-09-23 201 views
1

我在我的項目中使用Xamarin Forms。將Android View作爲自定義控件集成到Xamarin.Forms

基本上我想在我的表單中整合一個custom control。 此組件與Android視圖(axml)一起提供,我無法找到將其包含到我的共享Xamarin Forms項目中的方法。

我試圖創建一個自定義呈現:

[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); 
     } 
    } 
} 

沒有幫助我,因爲我沒能獲得Android的看法,與它互動,包括在我的形式。

我也看過如何將Android.Views.View轉換爲Xamarin.Forms.View,但我找不到這樣做的方法。

將自定義控件集成到Xamarin Forms應用程序的方式是什麼?

回答

2

你開始正確的方向。覆蓋OnElementChanged,因爲你應該創建你的Android視圖。

喜歡的東西:

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趕上RadialControl的屬性來更改你重寫OnElementPropertyChanged;

相關問題