Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Native classes without Custom Renderer

Poli97Poli97 Member ✭✭

Is there a way to use Android and IOS native classes (Views like Android.Button, IOS.UIButton ecc...) in Xamarin Forms without creating a Custom Renderer?

Answers

  • Poli97Poli97 Member ✭✭

    Already seen that documentation but I find out it is old and obsolete as it doesn't use PCL. So now with PCL I am no more able to reference IOS and Android from Forms

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    If you look at the samples a little closer, you can find ones like this

    The Forms project there is .NetStandard, the successor to PCL. Browse the code and see if this methodology fits your needs.

    There is also the sample mentioned in this thread, with the caveats mentioned as well.

  • Poli97Poli97 Member ✭✭

    @AdamMeaney said:
    If you look at the samples a little closer, you can find ones like this

    The Forms project there is .NetStandard, the successor to PCL. Browse the code and see if this methodology fits your needs.

    There is also the sample mentioned in this thread, with the caveats mentioned as well.

    As I read in this thread, they can only be used in XAML. For my purpose I need to use them in code. Is it impossible than?

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    From my understanding, without using a Shared Project it isn't possible to do it in code at this time.

    I will say that in my 6 years of Xamarin development, I have never thought that doing native views like this was more useful than renderers for the same scenarios.

    But I think you could easily make a view in xaml that is the Android Button and iOS button you want using the above strategies, then reference that view in the code instead. Basically a composite view holding the native views, if for some reason you need to use code and not xaml on a specific page.

  • Poli97Poli97 Member ✭✭
    edited January 29

    @AdamMeaney said:
    From my understanding, without using a Shared Project it isn't possible to do it in code at this time.

    I will say that in my 6 years of Xamarin development, I have never thought that doing native views like this was more useful than renderers for the same scenarios.

    But I think you could easily make a view in xaml that is the Android Button and iOS button you want using the above strategies, then reference that view in the code instead. Basically a composite view holding the native views, if for some reason you need to use code and not xaml on a specific page.

    Basically what I need to do is to use a native view because I need to override the accessibility methods (OnElementChanged() for Android and AccessibilityElementDidBecomeFocused() for IOS). And I need to add some of that views dynamically, this is the reason why I can't use XAML. I already tried to do that with Custom Renderer and I was able to override that methods, but I don't know why it seems that they never get called. So I thought it was a Custom Renderer problem.

    Here an example of a custom renderer I tried to create for android. I am able to use it in the Forms (infact I can see a red button and the console log "PIPPO created from Android"), but as I said OnElementChanged never got called (I can't see the console log "PIPPO I am in focus")

    ` [assembly: ExportRenderer(typeof(ICustomViewRenderer), typeof(AndroidCustomView))]
    namespace CustomViewAccessibility.Droid
    {
    public class AndroidCustomView: ButtonRenderer
    {
    public AndroidCustomView(Context context) : base(context)
    {
    }

        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
    
            if(Control == null)
            {
                return;
            }
    
            Console.WriteLine("PIPPO created from Android");
            Control.SetBackgroundColor(Android.Graphics.Color.Red);
        }
    
        public override void OnPopulateAccessibilityEvent(AccessibilityEvent e)
        {
            base.OnPopulateAccessibilityEvent(e);
            Console.WriteLine("PIPPO i am in focus");
    
            if (e.EventType == EventTypes.ViewAccessibilityFocused)
            {
                Console.WriteLine("PIPPO i am in focus");
            }
        }
    }
    

    } `

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    You can add a Xaml View dynamically. You just have to define it like you would any other custom view, then add it to the page like you do standard buttons dynamically.

    I am not too familiar with accessibility stuff, but I do know Xamarin had some basics implemented at some point. Maybe they are interfering with you in the renderer.

  • Poli97Poli97 Member ✭✭

    @AdamMeaney said:
    You can add a Xaml View dynamically. You just have to define it like you would any other custom view, then add it to the page like you do standard buttons dynamically.

    I am not too familiar with accessibility stuff, but I do know Xamarin had some basics implemented at some point. Maybe they are interfering with you in the renderer.

    Ok thanks for your help I try that way. By adding Xaml view dynamically you mean something like this?
    string navigationButtonXAML = "<Button Text=\"Navigate\" />"; Button navigationButton = new Button().LoadFromXaml(navigationButtonXAML); _stackyout.Children.Add(navigationButton);

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I mean unless you are entirely defining a view based on crazy configuration from some server, no.

    More like:

    <StackLayout x:Class="namespace.blah.MyButtonReplacementView"
        ... >
        <androidnamepsace:Button />
        <iosNamespace:Button />
    </StackLayout >
    

    And then in the code of your page that you are dynamically adding magic buttons to,

    var stack = new StackLayout();
    if (conditionForAddingButton)
    {
        stack.Children.Add(new MyButtonReplacementView());
    }
    
  • Poli97Poli97 Member ✭✭

    @AdamMeaney said:
    I mean unless you are entirely defining a view based on crazy configuration from some server, no.

    More like:

    <StackLayout x:Class="namespace.blah.MyButtonReplacementView"
        ... >
        <androidnamepsace:Button />
        <iosNamespace:Button />
    </StackLayout >
    

    And then in the code of your page that you are dynamically adding magic buttons to,

    var stack = new StackLayout();
    if (conditionForAddingButton)
    {
        stack.Children.Add(new MyButtonReplacementView());
    }
    

    Ok I will try thank you

Sign In or Register to comment.