Saturday, June 28, 2014

Running Gone Bananas on VS 2013

Way back in the days, we're talking years here, I played around with Cocos2D. The native c++/objective-c version that is. I loved it until my project suddenly stopped building and my patiens ran out. That was about the time I picked up MonoTouch instead.

Just the other day I saw the link to this page on the Xamarin web site.

I figured I give it a try to see if I could get it running.

I ran into a couple of issues on the way.

1. The git-stuff didn't work as expected

Disclaimer: This is probably due to my small experience with git.

In the beginning of the tutorial there is a link to clone the git repository for Cocos2D-XNA. I followed it and got started. The first git command under "Code on GitHub" worked fine. 

Also the sub module initialization and update worked great.

$ git submodule init and $ git submodule update

BUT, the external dependencies update didn't work out that great for me.

$ cd MonoGame
$ git submodule init
$ git submodule update ThirdParty/Libs

I got a "You need to run this command at the toplevel of the working tree" error.

Is this a problem? I don't know? :D If someone reading this knows the answer, please comment to enlighten a poor git fool.

2. Downloaded the bananas project

Yup, did so and unzipped it into my favorite folder. Had to move the project around to get the correct relative path to Cocos2D-xna since it loads some of the Cocos2d-xna projects. Not an issue, but it could be explained somewhere that you need it to be relative to the Cocos2D-xna folders.

3. Cocos2D-xna does not build

In the Cocos2d.ios project there is a class named CCTextFieldTTF. It cannot resolve a namespace reference to Microsoft.Xna.Framework.GamerServices. So what I did was to add the !IOS compiler directive to simply not include it for my iOS configuration. I also have to correct this in like five other places. It does build and run after that.

    using Microsoft.Xna.Framework.GamerServices;

After that, all success and I could collect as many bananas as I'd liked.

Monday, June 23, 2014

Exploring Xamarin Forms

WARNING: This post is updated as I go along. All of the content is subject to change. Please comment if I'm doing something wrong. :)

Last update: 2014-08-31

I did a post a couple of weeks ago about learning Mvvm-Cross (still on-going). This post is the Xamarin Forms equivalent. It's going to be unstructured and updated as I go along.

I like the Xaml-approach so I'll try to do as much as possible using Xaml.

My app doesn't do anything special yet, more than testing out different scenarios and currently only for iOS.

1. Added SQLite [SOLVED]

I followed my own post from a couple of months ago. Had to alter it a bit and introduce a ISQLiteConnectionFactory with a single Create-method that creates a device specific SQLiteConnection on each platform. This could also easily be resolved using a simple Inversion of Control container. Some of the work in Xamarin Forms Labs (number 3) might be of interest here later on.

UPDATE: When I try to run this on an actual device I get the following exception: SQLite.Net.SQLiteException: Could not open database file (CannotOpen). Turns out I forgot to put the database in the Personal folder...

2. Scrolling editors into view [WORKAROUND]

I created a page with an image at the top that is going to represent an image captured by the camera. Below that I added a Editor-object. The problem is that the keyboard hides the text field. So my first mission is to figure out how to fix this.

My first try was to add ScrollView around the content. This kinda solved the problem half-way. The keyboard still covers the editor but you can manually scroll it up into view. If I could get it to bring the focused control into view, I'd be a happy camper.

Asked a question on stack overflow to hopefully resolve this issue. The answer was that the ScrollView should work but that there is an open bug as of June 2014 that prevents this. So I'll just have to wait for the time being.

3. Camera access [SOLVED]

So, how is this done in Xamarin Forms. Is there a special "forms" way or does the old way work? Quick reply from twitter pointing me towards Thanks @mitchmilam! This seems to be very early on in development though. I'll keep an eye on it to see how it evolves.

Update 2014-07-20: Did finally download and check out the code! Seems really nice! I'll try to dig deep into it in the future. This is the kind of project I really would like to contribute to. For now I'll just use it and I'll start with the camera access.

I used the pre-release nuget packages instead of building my own. Install using

    install-package Xamarin.Forms.Labs -pre

on each platform.

It also seems they got a little more stuff in the wiki at last. I followed the example and get a Null Reference Exception trying to resolve the IMediaPicker.

    var picker = DependencyService.Get();

It's not registered. Adding the following code to AppDelegate.cs (for ios that is) did the trick.

     var resolverContainer = new SimpleContainer();
     resolverContainer.Register(t => AppleDevice.CurrentDevice);

4. TabbedPage stopped working on simulator

I've been working on some camera stuff so I used a real device to try my code out. I then switched back to the simulator but the main tabbed page just renders completely white. No clue yet on what caused this, but I'm not going to get to the root of this problem just yet.

Updated 2014-08-05: I was missing a low res version of an icon (png image) located in the iOS Resources folder. It seems like the simulator (or something in Xamarin Forms) doesn't use the hires image but the device does.

5. Intellisense for xaml [WORKAROUND]

Ok, I'm starting to need this. Unfortunately it may take a while to fix. Looks like MS has to change some stuff regarding PCL projects.

Workaround: Use Xamarin Studio instead...

6. Toolbar Item [SOLVED]

Having real trouble to simply add a Save-button to the toolbar/titlebar. It simply does not show. I had to ask a question to StackOverflow for this. Hopefully someone smarter will solve it soon.

2014-08-05: The problem was that the page was in a tab. I guess I have to use the Xamarin Forms Labs extended tabbed page to be able to determine when tabs are focused. I also wonder how the title bar is supposed to behave when switching tabs? It seems to belong to the TabbedPage, but I would like it to belong to the child tab being displayed? 


All my icons are grey squares. I really have to start reading the Images documentation. I follow all the advice but all my icons are still grey and squary.

8. Grouped Lists [STILL WORKING ON IT]

This is going to be my next challenge. I'll spend the day in the sun and doing my research on how this is done in Xamarin Forms.

9. Nuget-reference explosion [SOLVED]

Updated the Xamarin.Forms nuget package to version and it wouldn't build anymore. Both Xamarin Studio and Visual Studio told me to enable package restore since a package was missing. Didn't help. It turned out to be crap left in the project file as described in this post. The short version, you need to remove lines that look like this " Condition="!Exists(... " and that checks for the existence of a now old version of Xamarin Forms.

10. Provisioning profile issues

Not an Xamarin.Forms related issue, but it still slows me down...

Friday, June 13, 2014

Fragment as usual and as a Dialog

Short version

If you need to display a fragment as a usual fragment and as a Dialog. Just inherit it from DialogFragment. Call Show() if you want it as a dialog or add it as usual if you want it to behave as it would as a standard Fragment.

Long version

I spent a good hour trying to solve this in the most complicated ways imagenable. Now I'm just ashamed that I didn't try the most obvious.

I have an Android app that supports all kinds of screen sizes. One of the features is to do a stock search. On the phone it's a straight forward page by page search. Select the product, select some parameters, view result, view details. Each in a different fragment using the entire screen estate.

However, on a tablet with more space available the layout is different. The three first fragments are layed out to share the screen and the fourth (the details view) should be a dialog.

I tried to create a generic dialog wrapper to host my fragment. Of course, this did not work.

The solution, however, is quite simple.

Just inherit your fragment from DialogFragment instead of Fragment.

   1: if (_isDualPane)
   2: {
   3:     detailFragment.Show(this.SupportFragmentManager, "result_dialog");
   4: }
   5: else
   6: {
   7:     var ft= this.SupportFragmentManager.BeginTransaction();
   8:     ft.Replace(Android.Resource.Id.Content, detailFragment).AddToBackStack(null);
   9:     ft.Commit();
  10: }

The original stack overflow question

Wednesday, June 11, 2014

Access to path denied when adding axml-file in Visual Studio


This is a designer issue and occurs when you have multiple layout files. Like one in Layout and one in Layout-large. You need to check out both files from TFS. Also it should only occur when using Server Workspace. Local workspace should solve the problem by design.

Original post

This is something that happens from time to time for me.
  1. I add a new layout file in the layout-large (with a corresponding file in the layout folder) and it gets marked for adding to TFS.
  2. Using my extraordinary design skills I edit the layout in the designer.
  3. When I try to save it I get a "Access to the path '{path}' is denied.

The current setup is
  • Visual Studio 2012
  • Xamarin 3.0
So what is locking up my file? 
  • Tried to use SysInternals Handle tool, but no matching handles found there. 
  • Shutting down Visual Studio didn't help, still not able to edit/save it using Notepad++.
  • Created a test file in the same directory, just to check the directory security settings. No problem there.
  • Deleted the file in explorer and added a copy of it again. This somehow works...
I can now edit the file in notepad++ but not using the designer.


My workaround is to open the axml file with the Xml Editor instead by right-clicking and selecting Open With... Since I can't pinpoint the exact problem this will have to do it for now.

Friday, June 6, 2014

The many hoops of MVVMCross on iOS

EDIT: I just lost section 10-13 due to a server error. Thanks blogger... I can't remember what was written. :)

WARNING: This is a very unstructured post following my first attempts of creating a MvvmCross application using Xamarin.ios. As I go along with this, I'll try to restructure it.

This post is all about the hoops I had to jump to get started With MvvmCross for iOS using Xamarin.

I'm using the 3.1.2-beta1 nuget package available at This is a beta and I'm sure a lot will change before the stable release.

I have a simple solutions structure with a shared PCL core project and a platform specific project for each target device.

1. A PCL profile that works

The nuget package doesn't install using the profile I usually use.

I started by setting up a core PCL project using the following profile.

This will give you some headaches if you plan to use HttpClient later on. I'll update this with a solution when I cross (no pun intended) that bridge.

I then installed the MvvmCross nuget package by typing

install-package mvvmcross -pre

in the Package Console Manager.

2. Create the ios project

Don't use storyboarding. It doesn't make sense if you're using MvvmCross.You can use it if you really must but this post explains why it doesn' fit well.

I ended up creating an empty universal ios project and installed the MvvmCross nuget package using the same install-package command described in the first step.

Instructions for how to bootstrap your MvvmCross solution can be found in the ToDo-MvvmCross folder. It's basically tells you to reference your core project and the replace the code in the AppDelegate.cs file with the code found in AppDelegate.cs.txt.

3. Learn the naming Conventions

Create the ViewModels in the core project and a corresponding View in the platform specific project. They magically find each other. This is really simple MvvmCross stuff but it's one of those things that might get you off track.

If my viewmodel is named TestViewModel then the view must be TestView.

4. Binding to the ViewModel

The outline is to add a ViewController and make it inherit from MvxViewController. Add controls (or subviews) to the xib-file in xcode and create references by ctrl-dragging them into the .h file.

When I created my first controller I had trouble finding the CreateBindingSet method. It turns out it's an extension method living in Cirrious.MvvmCross.Binding.BindingContext. So simply add a using statement like the one below.

using Cirrious.MvvmCross.Binding.BindingContext;

The this.Bind(...) method, as found in most samples, isn't there any more.

So it would look something like this in the ViewDidLoad override.

var set = this.CreateBindingSet<TestView, Core.ViewModels.TestViewModel>();
// set.Bind(label).To(vm => vm.SomeProperty);
Oh, and also put the binding stuff below the call to the base method.

5. Mac jumping

I prefer using Visual Studio for coding so I have my development environment set up using a virtual machine through Parallels. I was first really glad to learn about the ios designer that came along with Xamarin 3.0, but unfortunatly it doesn't work on xib-files, only storyboards. *wierd*

So, I had to jump back to my MacOS and open up Xamarin studio. From there I opened the test project by navigating into the windows disk from the desktop. It opened up just fine. So when ever I need to fiddle with the xib-file I jump back into MacOS, double click the file in Xamarin Studio and edit it in Xcode. Nice and simple. :P

6. I want to bind a table view to something

I added a TableView to my TestView.xib file and created a reference to it. (in xcode).

Since it's hard to find any examples of anyone using the CreateBindingSet<> thingy I started to convert some earlier code samples.

I hit a rock wall. Consider the code below.

var source = new MvxStandardTableViewSource(MainTableView, "TitleText Name;");

set.Bind(source.ItemsSource).To(wm => wm.Persons); // NOT WORKING
set.Bind(source).To(wm => wm.Persons); // WORKS FINE


First thing, there is no error on failed bindings. Why not? I'm sure there is a good reason for it, but silent errors doesn't make you happy when you're unfamiliar with a framework.

Second thing, you apperantly bind directly to the source and not the source.ItemsSource. After that it works fine. I tried binding to simple arrays of string using a binding of TitleText only and to a list of custom objects using TitleText Name as the binding string.

7. You lose access to your windows disk from mac when sleeping


Not verified more than once. I switched over to MacOS and put the computer to sleep, still running Windows 8.1 in Parallels. When I resumed it the day after, Xamarin Studio was unable to find the project on Mac since I couldn't browse the windows disk from mac.

The solution was to restart the virtual machine. I'll follow this up if it happens again and try to come up with a solution.

8. MvxRelayCommand is now MvxCommand

It just is.

9. Lambda expression not working when binding to SelectionChangedCommand

Resolved: Just add a reference to System.Windows.

When trying to bind to the SelectionChangedCommand of the MvxStandardTableViewSource the following doesn't compile.

set.Bind(source).For(s => s.SelectionChangedCommand).To(vm => vm.PersonSelectedCommand);

But this do and works.

set.Bind(source).For(s => s.SelectionChangedCommand).To("PersonSelectedCommand");

The first fails with the error

Cannot convert 'lambda expression' to non-delegate type 'string'

I personally would prefer the first version to work to avoid compile time errors. But it's only a nice-to-have thing since there is a lot of other loose bindings around.

That's it for now

I do realize that this is a messy post and I'll do my best to replace it with a proper one after I get a full grip on this, if possible.

Thursday, June 5, 2014

Xamarin, hard to promote?

I've been working with Xamarin since the MonoTouch days and I've always found it to be a great product. The buzz around it is huge at the moment but the pricing still makes it very hard for a self-appointed Xamarin ambassador like myself to get people to try it.

-"Use the free version", they tell me.

Well, a major selling point for using Xamarins stuff is the Visual Studio integration. Don't get me wrong, Xamarin Studio is very competent but developers who's using VS don't want another IDE.

-"There is a 30 day (90 day with MSDN) full trial ", they continue...

Sure, but it's not enough time for most full time working developers who wants to learn something new on their spare time. Remember, they must learn iOS and Android SDKs as well.

So please, let one of these things happen:
  • Microsoft, just buy them and integrate it into the MSDN subscription. This is your chance to embrace a huge market share for developing for Android and iOS
  • Extend the trial to 180 days to make it easier for us to promote the product
  • Lower the price per seat for Business
  • Allow for VS integration in the free version
Yup, that's pretty much my solution for everything. Have a good one...