Xamarin.Forms + MVVML + Commands

I was recently browsing stackoverflow.com and ran across a question asking about commands using Xamarin.Forms. I decided to do a quick tutorial to explain what you need to know when you use commands and Xamarin.Forms.

For this example I decided to use MVVMLight since it provides a lot of the boiler plate code but any modern MVVM framework should do the trick. The stackoverflow question I mentioned was about passing parameters using Commands. The code sample provided in the question attempts to use an event handle in order to invoke a command but events are a completely different beast. The main difference between events and commands is that commands can be canceled while events cannot.

As I was saying you need an MVVM framework that supports generics in order to support parameter passing of a certain type. Since MVVMLight is open source you can see an example of this here. As you can see, the generic version of ICommand allows you to pass in the TYPE of property you want to pass along with it.

Now lets get into the code!

I first created a standard Xamarin Forms project using Xamarin Studio. It should be pretty straight forward if you go through their new project creation wizard. Make sure you include the MVVMLIght libraries if you wish to follow my example.

My page just consists of an Entry field to enter a ToDoItem, an add button, and a listview that will list the ToDoItems. These controls contain bindable properties that can be set using the ViewModel posted below.

I also created a View Modal that contains my command which adds to the todo items list.

I created a TodoTitle property that implements INotifyPropertyChanged. I'm using a helper method created by MVVMLight to quickly accomplish this. I also created a RelayCommand with the TYPE of string. This will allow us to automatically cast the parameter to its appropriate type. Since the RelayCommand takes and action as its parameter, you can simply pass in your method into the constructor.

As you can see in the Xaml, I have bounded the TodoTitle to the button as the CommandParameter. This allows me to pass along the text string through the RelayCommand which then gets passed down to my method to do what I want with it. Now you have a fully functioning Todo list! To browse the full code please visit my github account here.

Swagger + Swashbuckle + Web Api + Owin

I have recently started working on a side project of mine and I have decide to go with using Azure API apps + Web API for my API and angular for my front end. While publishing my app it suggested I use Swashbuckle to document my API . Since the default  Swagger config does not account for OWIN I was having trouble navigating to the main screen.  

First had to remove the assmebly startup code in the config. Then I had to edit my Register function to taken in HttpConfiguration object in order to use one created on start up.

Next all I do is pass in the HttpConfiguration in from my startup class.

Atlanta Rail and Bus App

So I have been messing around with Xamarin.Forms recently and decided to make a simple app based off it. My goal was for this app was to find an api which I could integrate into the application. After searching around I decided to go with the Marta Api in combination with the OneBusAway API. The combination of the apis allowed me to have more than enough information to build a transit application. The first release of the app was fun and challenging but there was still more I wanted to accomplish with it. I feel like the UI/UX experience can be cleaned up some but I wanted to release it so that I could update the app incrementally. I will be writing up some more technical posts on some of the challenges I had working with the Xamarin.Forms Platform.

As for the apps the links are available below: 

WP8 SQLite error: The specified module could not be found

Recently ran into this error when trying to setup SQLite on Xamarin.Forms for Windows Phone 8.

After trying to figure it out I finally found a post on stackoverflow that helped me with this issue. The solution was:

  1. Delete all references to sqlite from solution
  2. Install Sqlite for WP8 from http://visualstudiogallery.msdn.microsoft.com/cd120b42-30f4-446e-8287-45387a4f40b7
  3. Restart Visual Studio. In my case it's Visual Studio 2013.
  4. open your solution where you want to use sqlite
  5. Using package manager console in order to install sqlite-net. Enter the following command: Install-package sqlite-net
  6. Install sqlite-net-wp8. Enter the following command: Install-package sqlite-net-wp8
  7. Add compilation cofiguration

    7.1 Right-click the project and select Properties.

    7.2 Select the Build tab.

    7.3 In the Configuration list, select All Configurations.

    7.4 In the Platform list, select All Platforms.

    7.5 On the Build tab, you’ll see Conditional compilation symbols under the General header, containing a default value of SILVERLIGHT;WINDOWS_PHONE on a Windows Phone app project. Change the value to SILVERLIGHT;WINDOWS_PHONE;USE_WP8_NATIVE_SQLITE and save the project file.

  8. Right-click References and select add reference.Go to Windows Phone -> Extensions and check the option SQlite for Windows Phone

You can find the post here.