Xamarin.ios Encapsulation Delegate Events

Source: Internet
Author: User
Tags eventbus

A few days ago, the blog, customized a Portfolio view, because of the C # event mechanism does not understand, so the View button callback controller way I chose to develop Android often used interface callback. After my colleague pointed out the problem, I looked up some information and read some information about the C # entrustment mechanism in several books. Finally encapsulates the delegate event bus, Java is slowly imitating this delegation mechanism.

The encapsulated classes and how to use them are posted below.

The class that encapsulates the delegated mechanism first:

Using System;
Namespace Ruilaigrow.ios
{
	///<summary>
	///Delegate Event bus base class
	///by GE
	///</summary>
	public class Eventbus:eventargs
	{public
		Eventbus (string bus)
		{this
			. Bus = bus;
		}

		public string Bus {get; set;}
	}

	public class Eventbusdealer {public
		event eventhandler<eventbus> Newbusinfo;

		public void Newbus (object sender, String bus) {
			raisenewbusinfo (sender, bus);

		protected void Raisenewbusinfo (object sender, String bus)
		{
			eventhandler<eventbus> newbusinfo = Newbusinfo;
			if (newbusinfo!= null) {
				newbusinfo (sender, New Eventbus (bus));}}}



In the post use method, for example, the scene: If now in my custom view has a button, in his click event callback to the interface of a few data.

The first step is to instantiate the encapsulated words in a custom view to delegate event objects:

		Delegate event public
		Eventbusdealer Eventbus = new Eventbusdealer ();
In the Click event:

Button.touchupinside + = (sender, e) =>
					{
						eventbus.newbus (_list[index], "");
					

Use view in the interface controller to find the object of the delegate event and bind the event:

ListView.eventBus.NewBusInfo + = (object sender, Eventbus e) => { 
				//event sent here
			};

The Eventbus I encapsulated above (Android I have been using is eventbus, so from the perspective of the feelings of this, do not mislead people. I write my own iOS demo, the program's icon has been using Android's small robot icon, haha ha) sent the second parameter is string, you can modify to achieve their own needs.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.