Monthly Archives: April 2017

Trigger PropertyChanged of indexed property in Xamarin Forms

Xamarin Forms supports binding to indexed properties. But do you know how to trigger a PropertyChanged event on such a property so that the binding engine gets the updated value?

Binding to indexed properties

Binding to an indexed property in Xamarin Forms is simple. Imagine my ViewModel has an indexed property:

I can simply bind to it using the following syntax: <Label Text="{Binding [123]}" />  where 123 is the index I pass to the property.

Notifying the UI

If I now update the underlying data collection (for example: Data["123"] = "Ninjas"; ), my UI won’t be notified about this change. However, I can notify my UI by triggering the PropertyChanged event, passing ‘Item‘ as property name:

You can also use the following syntax if you only want to notify the UI about one particular element that has changed:

Easy, right? You just have to know it… 😉

 

If you want to try it our yourself, here is the complete code:

 

 

Setting up a Realm Object Server in Azure and connect to it with Xamarin Forms

Lately I’ve been playing around with Realm. Realm is a very cool platform that allows us to easily build mobile apps that use things like local data storage, two-way data synchronisation, real time collaboration, … Things that typically require some coding if you want to do it yourself!

After doing some test with local data storage (I might put up a blogpost about that as well…), I wanted to do some tests where I’d store the data remotely in a Realm Object Server. But, for that ofcourse, I would need to set up my own Server in Azure… And I was surprised about how easy and quickly I could do this, so I thought I’d share this with you together with a small Xamarin Forms app that connects to this datastore.

Setting up your VM

Go to the Azure Portal, select New, Compute and then select Ubuntu Server 16.04 LTS in the list. A new blade will open with some additional information, just click Create.

Next you will need to fill in some stuff to configure your Virtual Machine give it a Name, select the VM Disk type (I selected HDD as it isn’t ment to be a production sever or something, so performance isn’t that important now), type in a Username, Password, add it to an existing Resource group or create a new one. Click OK. 

Now it is time to choose the size of your Virtual Machine. It’s not the size that matters, they say…  Well for now, as I’m not setting up a production server or anything, I’m fine with one of the smaller VMs available in the list. I took an A1_V2 standard with 1 core, 2 gigs. Click Select.

The next blade opens up where we can configure some additional settings. I’m not an IT-Pro guy, so most of the things listed here are Chinese to me, so I just clicked OK.

Finaly we get a little summary, so just click OK.

While Azure is deploying our Linux Virtual Machine, please make sure you have the Linux Bash On Windows installed on your machine (or a Git bash or something like that).

Installing the Realm Object Server on your Linux VM

Once Azure has deployed your VM, we can install the Realm Object Server on it. In Azure, go to you VM and click the ‘Connect’ button.
Linux VM Connect

You should see the address you can use to connect to your VM using SSH.

Open Bash on your machine and SSH to your VM. Answer ‘yes’ and type in your password.
ssh to VMNow you can install the Realm Object Server, using the guide on the Realm website: Install Realm Object Server

On our Ubuntu machine, we want to execute the following commands:

At this moment, our Object Server is running on our VM, but we can’t connect to it just yet. That’s because, by default, Azure only allows an SSH connection to this VM. So we need to change that.

In Azure, use the search box at the top of the page, and type the name of your virtual machine. You should see your Virtual Machine itself together with its Network Interface and Security Group. Click the Network Security Group.
VM network Security Group

On the Network security group blade, click on Inbound security rules. On the new blade that opens up, click the Add button in the top left corner. Now you can configure the new Inboud Security Rule. I named this rule ‘Realm’, left everything to its default values, except for Port range. In the Realm documentation, we can read that all communication to the Realm Object Server is done over port 9080, so this is the port we would like to open. Fill in 9080 in the Port range field and click OK.

Initialize Realm Object Server

You should now be able to use your internet browser and browse to the IP address of your VM (the same you used for SSH) for port 9080 (http://VM-ipaddress:9080). On this page you can now configure an Admin user for your Object Server. Check the ‘I Agree’-box and click the Complete setup button.

Congratulations! Your Realm Object Server is now up and running! 🙂

On the Realm Object Server dashboard, go to Users and create a new User if we want.

Good news everyone: we can now start coding!

I’ll show a small example in Xamarin Forms: just a basic Task/ToDo list app that uses Realm to store its data localy and remote in our newly created Object Server.

Here’s what our Task model looks like:

For the Task object, I’ve also built a ViewModel:

The MainPage itself is just a ListView that shows all of my Tasks. At the bottom of the page, I have 2 Entries and a Button that I can use to add a Task to my list:

And my MainPageViewModel ties everything together:

 

Let’s take a look at some Realm specific code. Connecting to our Realm Object Server can be done by calling the static GetInstance method on the Realm object. As a parameter we can pass a SyncConfiguration . This SyncConfiguration  is nothing more or nothing less than the user that is connecting and the url to you Object server:

We can now use this Realm object to get our objects or to write, update or delete them from our data store.

You can find the code on my GitHub: SimpleRealmObjectServerExample

 

Enjoy!