Posted by ESCOZ on Tuesday, January 19, 2010
I have just pushed to github a port of the great TapkuLibrary’s LoadingHUDVIew control to MonoTouch. You can find the C# LoadingHUDView implementation here, or just download the entire solution to see the control working.
The code was translated mostly line-by-line, so the two classes still look very much alike. For comparison, the Obj-C code has around 130 LOC, plus another 10 for the Interface and around 20 of extension methods being used. The C# code has 140 in total. Not much of a difference, really. There’s quite a few things in the code that could be refactored, though, so the line count can probably be a bit lower than that.
To use the control, simply create a new object in code, and add it to the View, like below:
var hud = new LoadingHUDView("Loading", "This is the long message"); View.AddSubview(hud); hud.StartAnimating(); NSTimer.CreateScheduledTimer(TimeSpan.FromSeconds(5), ()=>hud.StopAnimating());
The code above will display a popup, and close it after 5 seconds. In the real world, you’ll likely call the StopAnimating() method after a web service returns, or a calculation completes, for example. Take a look at this Controller class for a better example of how to use the control.
MonoTouch makes it possible to load Objective-C compiled libraries to the compilation of the project, but C# bindings still need to be created to access those classes during runtime. The MonoTouch API documentation is fairly helpful; the only problem is it mentions a Binding Generator, but there’s no documentation for it in the wiki yet. Anyway, the major problem for me with that approach is that in order to make changes to the control, you have to fire XCode again, recompile, and then recompile in MonoDevelop. After doing that a few times, I just decided that translating the code was easier.