Protocol Delegate Pattern with Xamarin.iOS

FinHorsleyFinHorsley USMember ✭✭

What is the best way to replicate the Protocol Delegate pattern using .Net? I have seen examples using events, as well as examples using the native Delegates. Which is the best one to use, and if possible, could anyone drop in an example that I could have a look at?

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @FinHorsley,

    Check out the documentation on this top and let me know if you have further questions!

  • FinHorsleyFinHorsley USMember ✭✭

    Cheers! Had a look through the documentation although couldn't see anything with regards to creating my own protocols/delegates. (I know they are not part of .NET, so I'm guessing im asking what the alternative is). I'm trying to set up a protocol delegate pattern between a tableview and a cell so that when a user clicks a cell, the table controller can present an alert.

  • krobertsonkrobertson Member
    edited January 2018

    I have the same problem, the documentation only explains the C# side, and has no help for the Objective C side to connect bind to my own Objective C software. I need both sides, I can not find any help.
    How to make the code below work?

    // Objective C code

    #import <Foundation/Foundation.h>
    
    @class BindingTest;
    @protocol BindingTestDelegate
    @optional
    - (void) bindingTestDidReportMessage:(BindingTest *)binder;
    @end
    
    @interface BindingTest : NSObject
    {
        int      someNumber;
    }
    @property (nonatomic)id<BindingTestDelegate>delegateObject;
    
    -(id)init;
    - (void)testDelegate;
    
    @end
    
    #import "BindingTest.h"
    
    @implementation BindingTest
    
    -(id)init
    {
        self = [super init];
        if (self)
        {
            someNumber = 0;
        }
    
        return self;
    }
    
    -(void)testDelegate
    {
        if (nil != self.delegateObject)
        {
            [self.delegateObject bindingTestDidReportMessage:self];
        }
    }
    
    @end
    

    // C# code

    namespace client.iOS

    {
    
   [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    
   {
    
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
 
           {
 
               global::Xamarin.Forms.Forms.Init();

                LoadApplication(new App());

 
    
               // create the binding class and test a callback
               BindingTest bindingTest = new BindingTest();
                bindingTest.delegateObject += BindingTestDidReportMessage;
 
               bindingTest.TestDelegate();


    
               return base.FinishedLaunching(app, options);
    
       }



    
            public void BindingTestDidReportMessage(BindingTest binder, EventArgs args)
    
        {
                // I want to get here 
    
            int debugTest = 0;
    

            debugTest++;
 
               debugTest++;

                debugTest++;
 
               debugTest++;
 
           }

    }
    }

    
Sign In or Register to comment.