Creating an animated spinner in a Xamarin.iOS (MonoTouch) UIImageView

Background I’m well into my first week of building the Sierra Trading Post first iOS app using Xamarin.iOS and it has been a fun ride so far. One of the first things needed was a system for showing a loading image while asynchronously retrieving the final image with a web request. Attempt 1 Xamarin has a recipe for using a UIImageView‘s AnimationImages to make a spinner. UIImageView someImageView = new UIImageView(); someImageView.AnimationImages = new UIImage[] { UIImage.FromBundle(“Spinning Circle_1.png”), UIImage.FromBundle(“Spinning Circle_2.png”), UIImage.FromBundle(“Spinning Circle_3.png”), UIImage.FromBundle(“Spinning Circle_4.png”), }; someImageView.AnimationRepeatCount = 0; // Repeat forever. someImageView.AnimationDuration = 1.0; // Every 1s. someImageView.StartAnimating(); It may be possible to make this work, but it wasn’t quite what I needed. This seems to be more of an image rotation than an animation. As a result, it creates a jerky animation between the various images equally distributed over the AnimationDuration you set. After this, attempts to find some ideas for a better solution lead me to about a hundred lines of code that proved a difficult to consume, involving a CGBitmapContext and CGAffineTransform.MakRotation. (To be fair, this code isn’t doing something as simple as what I want to do.) Hoping to avoid that, I simply added four more… Continue reading

Mono for Android: “aapt.exe” exited with code 1

TL;DR Also available in TL;SO (too long; Stack Overflow) flavor. Getting this error: “aapt.exe” exited with code 1? Do you have any files in your Mono for Android solution that are being packaged together with the app (e.g., “AndroidResource” build action)? If so, make sure they don’t have anything but letters, numbers, periods, and underscores ([a-z0-9_.]) in their names. Details I am still getting my feet wet with Mono for Android (MfA) development. One of my first projects was a flashlight app for my Galaxy Nexus that introduced me to a couple Android/MfA development quirks. One concept in app development that I would like to explore is long-running tasks. How do I keep something going after the user has switched off to something else? Greg Shackles has a new Visual Studio Magazine article on Background Services in MfA on just that topic. He creates a background service for playing an MP3 file using a standard Android service. In his sample project, he sets up the service to play some Creative Commons Nine Inch Nails music. I’m betting that I’m the first person to use (and include in the sample) a Nine Inch Nails song for Visual Studio Magazine. #winning— Greg… Continue reading

Where did that JSON field go? Serializing IHtmlString to JSON.

TL;DR If your brain consumes Stack Overflow questions better than blog posts, go see “How do I serialize IHtmlString to JSON with Json.NET?” over there. IHtmlString doesn’t play nicely with JSON serialization If you have an IHtmlString in your JSON (regardless of arguments against putting raw HTML in JSON), you will probably need to customize the serialization to get the HTML out of that variable. In fact, the serialization will probably be invalid compared to what you expect; it does make sense if you think about how the serialization process works. Fortunately, putting together a quick Json.NET JsonConverter will make the issue go away. What you might expect from some object containing an IHtmlString variable named x: { x = “some <span>text</span>” } What default .NET JavaScript serialization and default Json.NET serialization will give you for said object: { x = { } } How to fix it with Json.NET: public class IHtmlStringConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(Type objectType) { return typeof(IHtmlString).IsAssignableFrom(objectType); } … public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { IHtmlString source = value as IHtmlString; if (source == null) { return; } writer.WriteValue(source.ToString()); } } Background While working on some random API, we… Continue reading

Performance Stub: getting all subtype items from a list

Performance Stubs These blog posts are simply times I wanted to identify the fastest method for accomplishing a particular goal. As I write the code, I like to make some light notes of alternatives while driving forward with the first implementation that makes it from my brain to my fingers. When I get the chance, I can go back and flesh out the two versions and drop them into some basic Stopwatch timing to determine which is better in terms of raw speed. Factoring those results with clarity of code, I have a method I will likely choose the next time I need the same feature. Goal Given a particular IEnumerable, find all the elements that are of a particular type (subclass, in this case). Method 1: Select As, Where Not Null The as operator with convert between two types with one nice tweak. If the cast cannot happen, it results in null. In this version, we massage the list by hitting all items with an as cast and then filter out the ones that didn’t cast successfully. public static IEnumerable<SubSomething> SelectAsWhereNotNull(IEnumerable<Something> source) { return source.Select(item => item as SubSomething).Where(item => item != null); } Method 2: Where Is, Cast… Continue reading

Closing and Re-opening tabs in Visual Studio with Ctrl+W

Visual Studio 2017 Undo-Close Update: The Productivity Power Tools have spun off into a bunch of more-focused extensions. To get Undo Close in Visual Studio 2017, you will want the Power Commands extension now. Visual Studio 2013 Undo-Close Update: Since the prior options for re-opening closed tabs fell apart with the release of Visual Studio 2013, you will need the newly released Productivity Power Tools 2013. Update: now with the ability to re-open closed tabs with Ctrl+Shift+T. This also allows you to re-open tabs closed by a project file reload, which is fantastic! Ever tried to close a tab in Visual Studio 2010/2012 with Ctrl+W. If so, you find yourself selecting the current word in your text editor (Edit.SelectCurrentWord). I don’t use that shortcut, though I could see it being handy over my usual Ctrl+Shift+Right-/Left-Arrow. I do, however, use Ctrl+W to close windows/tabs in just about every other program I use. In order to make that shortcut work for your Visual Studio editing, you just need to assign it to File.Close instead. For the visual, here’s a snapshot similar to what you will want (note: I already made this change before snapping a pic, so yours may look slightly different)…. Continue reading