I need to access the views created by Xamarin.Forms outside of the shared Xamarin.Forms code.
how to set an android id attribute in Xamarin.Forms?
one way of getting at the Android id is via a custom renderer. For instance, in case the view in question would be a Xamarin.Forms.Editor, you could add a custom renderer to your app as follows:
First, in your Xamarin.Forms project, add a class that derives from the view you'd like to get the id from:
namespace MyApp
{
public class CustomEditor : Editor
{
}
}
Note that this class doesn't add any new members, it's just there to define a new type that we will need to associate the custom renderer with.
Then, in your Xamarin Android project, add the custom renderer and associate it with your custom view type:
using System;
using MyApp;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace MyApp.Android
{
class CustomEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Editor> e)
{
base.OnElementChanged(e);
int androidId = this.Id; // android widget id
Guid xamarinId = e.NewElement.Id; // xamarin view id
}
}
Within that custom renderer you have now access to the underlying Android widget that is associated with the Xamarin Forms view. This means you can get at the Android .id property as well.
Please note:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With