Calling a custom method on a custom PageRenderer

JoshuaNovakJoshuaNovak USMember ✭✭

I am using a custom page renderer so I can access PresentViewController, but all the examples i've seen call functions on ViewDidAppear. I would like to be able to call it whenever I want, for example on a button click. Is there a way to access my ShareOnFacebook method from my c# project?

Here is the code for my custom renderer.

using System;
using Xamarin.Social.Services;
using Xamarin.Social;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using SocialTest.iOS;
using SocialTest;

[assembly: ExportRendererAttribute(typeof(SocialPage), typeof(SocialPageRenderer))]

namespace SocialTest.iOS
{
    public class SocialPageRenderer : PageRenderer
    {
        public SocialPageRenderer ()
        {
        }

        public void ShareOnFacebook()
        {
            // 1. Create the service
            var facebook = new FacebookService {
                ClientId = "facebook app id here",
                RedirectUrl = new System.Uri ("http://localhost:5000/oauth/callback")
            };

            // 2. Create an item to share
            var item = new Item { Text = "Xamarin.Social is the bomb.com." };
            item.Links.Add (new Uri ("http://github.com/xamarin/xamarin.social"));

            // 3. Present the UI on iOS
            var shareController = facebook.GetShareUI (item, result => {
                // result lets you know if the user shared the item or canceled
                DismissViewController (true, null);
            });
            PresentViewController (shareController, true, null);
        }
    }
}

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Define a C# event in your PCL Element class, and subscribe to that notification from your renderer.

  • JoshuaNovakJoshuaNovak USMember ✭✭

    @adamkemp Is there somewhere that I can see an example of this? I'm new to Xamarin/C# so i'm not sure how to go about doing that exactly.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Try this one.

  • JoshuaNovakJoshuaNovak USMember ✭✭

    @adamkemp I went through the article, but it just seems to be a regular custom renderer with some text being passed. I need to call a function in a custom page renderer, similar to a dependency service though as shown in my code.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    That example shows you what you need. Look at the renderer's OnElementChanged method and notice how it subscribes to the Element's Toggled event. When the element (the PCL class) wants the renderer to do something (update the state of the switch in this case) it invokes the event, and the renderer subscribes to that event. Essentially that allows the element to call a method in the renderer without directly referring to the renderer or the method. Events are a way to decouple things.

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    I have done this kind of thing by overriding OnElementPropertyChanged in the custom renderer, then checking the name of the property that changed to find out if the renderer has to update something. It's probably less efficient but to my simple mind it seems easier to understand, and if you browse the Xamarin.Forms source code it's a pattern that's heavily used there too.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    If the code lends itself to implementing as a property then that's a great way to do it. Sometimes it isn't natural to use a property with a value, and what you really want is just an action that says "do this now". In that case a new event is more natural.

Sign In or Register to comment.