How to execute a platform (Android) specific method within a ButtonOnClick() using a Xamarin.Forms custom renderer.










0















Introduction:



I am starting with code from:



https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Entry/Android



to study custom renderers. I am doing this because there is code that only executes on the android platform. Lets call this "androidMethod()" and belongs in the Android codebase (not the shared library). I have noticed that most of the examples I have found have utilized customRenderers for making platform specific UI modifications (like the example in the link) but I intend to make no changes to the UI, rather I am trying to place a platform specific method in a Xamarin.Forms ButtonOnClick() method as the code below indicates.



The code below is similar to the code you will find in the MyEntryRenderer.cs from the link but you will see that it was modified to apply to a button instead of an entry.



MyButtonRenderer.cs:



using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;
using Android.Content;

[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.Android

class MyButtonRenderer : ButtonRenderer

private Button androidButton;

public MyButtonRenderer(Context context) : base(context)



protected override void OnElementChanged(ElementChangedEventArgs<Button> e)

base.OnElementChanged(e);

if (Control != null)

//I want to be able to do something like this:
ButtonOnClick(androidMethod());






How do I get androidMethod(); to execute in this context. The samples I find are limited so please try to limit your response to something that would be compatible with the example. Thankyou!










share|improve this question






















  • if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer

    – Jason
    Nov 14 '18 at 18:07











  • Wow, you may have saved me. Was I mistaken to think that a custom renderer could do anything besides UI modifications?

    – spacetimeengineer
    Nov 14 '18 at 18:22











  • a Custom Renderer can do other platform specific things, but it's primarily geared towards UI

    – Jason
    Nov 14 '18 at 18:35











  • If you make a question, I give you the credit. Thanks.

    – spacetimeengineer
    Nov 14 '18 at 19:00
















0















Introduction:



I am starting with code from:



https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Entry/Android



to study custom renderers. I am doing this because there is code that only executes on the android platform. Lets call this "androidMethod()" and belongs in the Android codebase (not the shared library). I have noticed that most of the examples I have found have utilized customRenderers for making platform specific UI modifications (like the example in the link) but I intend to make no changes to the UI, rather I am trying to place a platform specific method in a Xamarin.Forms ButtonOnClick() method as the code below indicates.



The code below is similar to the code you will find in the MyEntryRenderer.cs from the link but you will see that it was modified to apply to a button instead of an entry.



MyButtonRenderer.cs:



using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;
using Android.Content;

[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.Android

class MyButtonRenderer : ButtonRenderer

private Button androidButton;

public MyButtonRenderer(Context context) : base(context)



protected override void OnElementChanged(ElementChangedEventArgs<Button> e)

base.OnElementChanged(e);

if (Control != null)

//I want to be able to do something like this:
ButtonOnClick(androidMethod());






How do I get androidMethod(); to execute in this context. The samples I find are limited so please try to limit your response to something that would be compatible with the example. Thankyou!










share|improve this question






















  • if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer

    – Jason
    Nov 14 '18 at 18:07











  • Wow, you may have saved me. Was I mistaken to think that a custom renderer could do anything besides UI modifications?

    – spacetimeengineer
    Nov 14 '18 at 18:22











  • a Custom Renderer can do other platform specific things, but it's primarily geared towards UI

    – Jason
    Nov 14 '18 at 18:35











  • If you make a question, I give you the credit. Thanks.

    – spacetimeengineer
    Nov 14 '18 at 19:00














0












0








0








Introduction:



I am starting with code from:



https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Entry/Android



to study custom renderers. I am doing this because there is code that only executes on the android platform. Lets call this "androidMethod()" and belongs in the Android codebase (not the shared library). I have noticed that most of the examples I have found have utilized customRenderers for making platform specific UI modifications (like the example in the link) but I intend to make no changes to the UI, rather I am trying to place a platform specific method in a Xamarin.Forms ButtonOnClick() method as the code below indicates.



The code below is similar to the code you will find in the MyEntryRenderer.cs from the link but you will see that it was modified to apply to a button instead of an entry.



MyButtonRenderer.cs:



using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;
using Android.Content;

[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.Android

class MyButtonRenderer : ButtonRenderer

private Button androidButton;

public MyButtonRenderer(Context context) : base(context)



protected override void OnElementChanged(ElementChangedEventArgs<Button> e)

base.OnElementChanged(e);

if (Control != null)

//I want to be able to do something like this:
ButtonOnClick(androidMethod());






How do I get androidMethod(); to execute in this context. The samples I find are limited so please try to limit your response to something that would be compatible with the example. Thankyou!










share|improve this question














Introduction:



I am starting with code from:



https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Entry/Android



to study custom renderers. I am doing this because there is code that only executes on the android platform. Lets call this "androidMethod()" and belongs in the Android codebase (not the shared library). I have noticed that most of the examples I have found have utilized customRenderers for making platform specific UI modifications (like the example in the link) but I intend to make no changes to the UI, rather I am trying to place a platform specific method in a Xamarin.Forms ButtonOnClick() method as the code below indicates.



The code below is similar to the code you will find in the MyEntryRenderer.cs from the link but you will see that it was modified to apply to a button instead of an entry.



MyButtonRenderer.cs:



using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using CustomRenderer;
using CustomRenderer.Android;
using Android.Content;

[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace CustomRenderer.Android

class MyButtonRenderer : ButtonRenderer

private Button androidButton;

public MyButtonRenderer(Context context) : base(context)



protected override void OnElementChanged(ElementChangedEventArgs<Button> e)

base.OnElementChanged(e);

if (Control != null)

//I want to be able to do something like this:
ButtonOnClick(androidMethod());






How do I get androidMethod(); to execute in this context. The samples I find are limited so please try to limit your response to something that would be compatible with the example. Thankyou!







xamarin.forms custom-renderer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 17:49









spacetimeengineerspacetimeengineer

1355




1355












  • if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer

    – Jason
    Nov 14 '18 at 18:07











  • Wow, you may have saved me. Was I mistaken to think that a custom renderer could do anything besides UI modifications?

    – spacetimeengineer
    Nov 14 '18 at 18:22











  • a Custom Renderer can do other platform specific things, but it's primarily geared towards UI

    – Jason
    Nov 14 '18 at 18:35











  • If you make a question, I give you the credit. Thanks.

    – spacetimeengineer
    Nov 14 '18 at 19:00


















  • if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer

    – Jason
    Nov 14 '18 at 18:07











  • Wow, you may have saved me. Was I mistaken to think that a custom renderer could do anything besides UI modifications?

    – spacetimeengineer
    Nov 14 '18 at 18:22











  • a Custom Renderer can do other platform specific things, but it's primarily geared towards UI

    – Jason
    Nov 14 '18 at 18:35











  • If you make a question, I give you the credit. Thanks.

    – spacetimeengineer
    Nov 14 '18 at 19:00

















if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer

– Jason
Nov 14 '18 at 18:07





if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer

– Jason
Nov 14 '18 at 18:07













Wow, you may have saved me. Was I mistaken to think that a custom renderer could do anything besides UI modifications?

– spacetimeengineer
Nov 14 '18 at 18:22





Wow, you may have saved me. Was I mistaken to think that a custom renderer could do anything besides UI modifications?

– spacetimeengineer
Nov 14 '18 at 18:22













a Custom Renderer can do other platform specific things, but it's primarily geared towards UI

– Jason
Nov 14 '18 at 18:35





a Custom Renderer can do other platform specific things, but it's primarily geared towards UI

– Jason
Nov 14 '18 at 18:35













If you make a question, I give you the credit. Thanks.

– spacetimeengineer
Nov 14 '18 at 19:00






If you make a question, I give you the credit. Thanks.

– spacetimeengineer
Nov 14 '18 at 19:00













1 Answer
1






active

oldest

votes


















1














if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306070%2fhow-to-execute-a-platform-android-specific-method-within-a-buttononclick-usi%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer






    share|improve this answer



























      1














      if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer






      share|improve this answer

























        1












        1








        1







        if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer






        share|improve this answer













        if you want to execute a platform specific method, I would use DepenencyService instead of a Custom Renderer







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 19:10









        JasonJason

        52.3k1290118




        52.3k1290118





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306070%2fhow-to-execute-a-platform-android-specific-method-within-a-buttononclick-usi%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo