简体   繁体   中英

Xamarin / Monotouch: Custom class events

I'm currently building an application for iOS and Android using Xamarin and MonoTouch. In the application there is going to be a lot of data loaded from JSON, and therefore I wanted to incorporate a unified loader, an object that runs on application start to check whether it needs to re-download information or not.

The loading class is done and is fully functional, and has the following methods that I want to be able to bind events to. See below:

  • BeginLoading
  • ReloadPosts
  • ReloadLayers
  • ReloadRunners
  • FinishedLoading

These are all self contained and run in the loader class which I initiate in ViewDidLoad in my main screen (MainScreen.cs) using the following code:

var loader = new UnifiedLoader();

This starts the process of checking the local cache, last reload time etc and either starts the reloading process - posts, layers, runners or jumps straight to FinishedLoading.

What I'd like to be able to do is to listen for these "events" in some fashion, and I have no idea how to go about doing so. Please look below for an example.

var loader = new UnifiedLoader();

loader.LoadingDidBegin += () => {
    Console.Out.WriteLine("Loading started");
    // Display spinner or something...
};

loader.DidReloadPosts += () => {
    Console.Out.WriteLine("Posts were reloaded");
    // Update reloading percentage, show user...
};

loader.DidReloadLayers += () => {
    Console.Out.WriteLine("Layers were reloaded");
    // Update reloading percentage, show user...
};

loader.DidReloadRunners += () => {
    Console.Out.WriteLine("Runners were reloaded");
    // Update reloading percentage, show user...
};

loader.LoadingDidFinish += () => {
    Console.Out.WriteLine("Loading finished");
    // Remove spinner, proceed...
};

As of now I have no idea how I would go about implementing these events in the loading class. I've been searching and going through the API documentation but found nothing to aid me.

I would be more than thankful if someone could help me solve this.

Thanks in advance, Jonathan

The preferred way would be to just write:

public EventHandler LoadingDidBegin;

This saves you from declaring the delegates and conforms to coding guidelines: http://msdn.microsoft.com/en-us/library/w369ty8x.aspx

I solved it by finding the Microsoft documentation for C# events. It was as simple as using the following code to register the event delegates and events.

This code goes outside of the class:

public delegate void LoadingDidBegin();

And this code goes inside the class:

public event LoadingDidBegin LoadingDidBegin;

And in the method where you want to invoke the event, call this:

// Trigger event:
if (this.CheckingDidBegin != null){
    this.CheckingDidBegin ();
}

And last, in the class where you bind the event, bind the delegate like this:

var loader = new UnifiedLoader ();

loader.LoadingDidBegin += delegate {
    // Do something here, show a HUD for instance...
};

loader.InitiateLoader ();

That's pretty much it, just remember to register the delegates before initiating the methods that carry the event triggers, otherwise they will just return null and you will get no feedback.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM