Skip to main content

Posts tagged with 'c'

I first used MvvmCross with Windows Phone 8, and XAML has a pretty good data-binding syntax already in place.

Switching over to Android, though, and the data binding didn't seem as straightforward. I was struggling just to bind a Button to an IMvxCommand.

So I checked out the MvvmCross documentation on Databinding (which, by the way, makes it seem like databinding is still very much a work in progress in MvvmCross), and I came across the idea of "Fluent Databinding" (as opposed to "Swiss Databinding", I guess). Instead of doing the binding in the AXML Android view markup, I could just do it in C# in the Android activity. Here's an example of binding a Button click event to an IMvxCommand in the viewmodel:

Note that the .For("Click") clause is actually optional, since "Click" seems to be the "default view property". But I chose to be explicit, especially for this example.

Welcome to another "Weekly Concerns". This is a post-a-week series of interesting links, relevant to programming and programmers. You can check out previous Weekly Concerns posts in the archive.

If you have an interesting link that you'd like to see in Weekly Concerns, leave a comment or contact me.

Ah ah ah, you didn't say the magic word!

In a project that I'm working on, I'm using Dapper for data access and I'm using StructureMap as my IoC containter. It's an ASP.NET MVC project.

I was recently demoing the project to my partner on the staging site (Azure), and I ran into a troubling error having to do with data connections (that I had not experienced running it locally on IIS yet). I immediately thought that it must be an issue with the SQL connections (SqlDbConnection objects that implement the IDbConnection interface, per Dapper) not being closed and/or disposed of correctly.

I remember encountering the same issue when I was developing this very site that you are reading, and the solution is to use the HttpContext scope within StructureMap when configuring it for the IDbConnection interface.

Here's what I started with:

And here's what I refactored it to:

I just added HttpContextScoped (the default is PerRequest scope, see StructureMap docs on Scoping and Lifecycle Management). I also added the ReleaseAndDisposeAllHttpScopedObjects to Global.asax.cs, so that StructureMap would...release and dispose those objects at the end of each request.

However, when I did that, I was still getting problems. I would get exceptions saying that the SqlConnection ConnectionString was the empty string. Something wasn't right. I did some searching around about the issue in the docs, and couldn't really find what I was looking for. I did come across someone with a similar issue on StackOverflow, also trying to understand HttpContextScoped. The top answer from PHeiberg (not the accepted answer) pointed me the right direction.

I used a lambda expression instead of just passing the SqlConnection directly. So now my SqlDbConnections are scoped to the HttpContext, and will be cleaned up after every request. That should squash the issue I was seeing on the staging stie.

I'm continuing my adventures into mobile development this year. I've spent a lot of time on Windows Phone 8 so far, and as I'm writing this post, I've shipped an app off to a couple of friends to try on their real phones.

Next step, since I'm using MvvmCross, is to take that codebase and use (most of) it to create an Android version of the same app using Xamarin.

I've done Android development before, both with plain Java and with Xamarin (called MonoDroid way-back-when), and either way a major pain point has been the Android emulator. It's slow, clunky, and cumbersome, and everyone knows it. Fortunately, Greg Shackles, Xamarin MVP and all-around great guy, heard my whining and recommended that I check out Genymotion (which I had never heard of).

And, behold! Genymotion is just what I've always wanted. It can create and spin up Android emulators for me using VirtualBox. The prices are very reasonble, and there's even a free version that is no slouch!

I installed it, and it was even kind enough to install VirtualBox for me. Once it's installed, you can select from a whole bunch of pre-configured Android devices (Galaxy S4, HTC One, Moto X, etc).

Genymotion has a lot of pre-configured devices for you to choose from

I decided to create a Galaxy S2 image (which is the phone I actually own and use everyday).

Genymotion Galaxy S2 ready to go in Genymotion

Then, just click that "play" button, and a reasonably fast Android emulator will start up. Both Xamarin Studio and Visual Studio with Xamarin discover it, no problem. I assume Eclipse will also be able to find it.

Xamarin Studio finds Genymotion just fine

If you're doing Android development, do yourself a favor and give Genymotion a try.

P.S. It's pronounced "Jenny motion"

Welcome to another "Weekly Concerns". This is a post-a-week series of interesting links, relevant to programming and programmers. You can check out previous Weekly Concerns posts in the archive.

If you have an interesting link that you'd like to see in Weekly Concerns, leave a comment or contact me.

Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter