Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a method in a base Frame Renderer from the iOS Custom renderer?

I have this code:

public partial class PhrasesFrameRenderer : Frame
{
    Random rand = new Random();
    private int answeredPhraseCount;
    private int correctAns;

    public PhrasesFrameRenderer()
    {
        InitializeComponent();
        App.PhrasesFrameRenderer = this;

        public void abc()
        {

            ....
        };

and the custom Renderer

[assembly: ExportRenderer(typeof(PhrasesFrameRenderer), typeof(PhrasesFrameCustomRenderer))]
namespace Japanese.iOS
{
    public class PhrasesFrameCustomRenderer : FrameRenderer
    {
        UISwipeGestureRecognizer leftSwipeGestureRecognizer;
        UISwipeGestureRecognizer rightSwipeGestureRecognizer;
        PhrasesFrameRenderer frame;
        bool rightSwipeEnabled = false;

Can someone explain to me how I can call the ABC function in the Phrases FrameRenderer from the custom Renderer code.

like image 983
Alan2 Avatar asked Jan 03 '23 18:01

Alan2


1 Answers

In your CustomRenderer the Element property is basically the view you declared in the PCL Xamarin Forms class in your case PhrasesFrameRenderer

You just need to cast this property to the class and you will have access to all the public methods and properties.

var frame = (PhrasesFrameRenderer)Element;
frame.abc();

or

var frame = Element as PhrasesFrameRenderer;

if(frame != null)
    frame.abc();

This should work.

One note: I would not call the Custom views as Renderer as this can create confusions with its actual renderer. Your custom frame could just be named PhrasesFrame

like image 53
pinedax Avatar answered Jan 06 '23 08:01

pinedax