fex
fex

Reputation: 3558

Xamarin - Custom Renderer - cast Form.View to Android.View

Is it possible to get Android.View (and equivalent view object in iOS) from Xamarin.Form.View? I have custom control which takes dependencies to Android.View. I am writing custom renderer which would set that custom control as native view but I need to convert my ContentView (exposed as BindableProperty, initialized in xaml) from Portable library to Android.View.

Upvotes: 3

Views: 4055

Answers (1)

SKall
SKall

Reputation: 5234

Yes you can. I have written an extension that uses reflection to get the underlying native view:

https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms.Droid/Extensions/ViewExtensions.cs#L54-L59

Native view in the code is Android.Views.View type and View is Xamarin.Forms.View.

https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms.Droid/Extensions/ViewExtensions.cs#L19

The same works for iOS as well, just cast it to UIView.

Upvotes: 3

Related Questions