When doing Xamarin.iOS (or just iOS) development, you often need a way to compute the size of a text, dependding of its content.

Well, in fact, this is pretty simple thanks to the class NSString and its method StringSize:

var titleFont = UIFont.FromName(ApplicationFonts.LightFontName, 15f);
var requestedSize = new NSString("Put your text here").StringSize(titleFont);

The method StringSize takes in parameter the font that will be applied to the text, and all the dedicated parameters (bold, italic, etc.). Indeed, according to the kind of font and the style applied, the height/width will not be the same every time.

Simple but works fine!

 

Happy coding!

For the project I’m currently working on (a Xamarin.iOs application), I needed to find a way to retrieve the mime type of a file, based on its extension.

After looking on Internet, I’ve found that I needed to use the classes UTTypeCopyPreferredTagWithClass and UTTypeCreatePreferredIdentifierForTag which are in MobieCoreServices. Unfortunately, this methods are not directly exposed by Xamarin so you can’t call these methods directly.

But, fortunately, thanks to the P/Invoke process, it’s still possible to use them!

Indeed, first, you need to declare the specific mapping to the native functions:

public static class NativeTools
{
    [DllImport(ObjCRuntime.Constants.MobileCoreServicesLibrary, EntryPoint = "UTTypeCopyPreferredTagWithClass")]
    public extern static IntPtr UTTypeCopyPreferredTagWithClass(IntPtr uti, IntPtr tagClass);

    [DllImport(ObjCRuntime.Constants.MobileCoreServicesLibrary, EntryPoint = "UTTypeCreatePreferredIdentifierForTag")]
    public extern static IntPtr UTTypeCreatePreferredIdentifierForTag(IntPtr tagClass, IntPtr tag, IntPtr uti);
}

Then, you just have to use the exposed functions in your own code:

public string GetMimeType(string fileName)
{
    var extensionWithDot = Path.GetExtension(fileName);
    if (!string.IsNullOrWhiteSpace(extensionWithDot))
    {
        var extension = extensionWithDot.Substring(1);
        if (!string.IsNullOrWhiteSpace(extension))
        {
            var extensionClassRef = new NSString(UTType.TagClassFilenameExtension);
            var mimeTypeClassRef = new NSString(UTType.TagClassMIMEType);

            var uti = NativeTools.UTTypeCreatePreferredIdentifierForTag(extensionClassRef.Handle, new NSString(extension).Handle, IntPtr.Zero);
            var mimeType = NativeTools.UTTypeCopyPreferredTagWithClass(uti, mimeTypeClassRef.Handle);
            using (var mimeTypeCString = new CoreFoundation.CFString(mimeType))
            {
                return mimeTypeCString;
            }
        }
    }

    return string.Empty;
}

Once executed, the code will get the correct mime type:

image

I hope that will be helpful to some of you!

 

Happy coding!

Today, I’ve worked on a Xamarin.Forms application and I wanted to use the following code:

<Label FontAttributes="Bold"
       TextColor="Gray" Text="{Binding CurrentUser.FullName, FallBackValue=Username}">
</Label>

Looks pretty simple for anyone working with XAML: if my CurrentUser.FullName is not available, I want to put the string “Username” in the Label.

Unfortunately, the previous code does not work because FallBackValue (and TargetNullValue) are not available on the Binding object (this appears to be a known issue to the Xamarin team for a long time now: https://bugzilla.xamarin.com/show_bug.cgi?id=22720).

So, to workaround the problem, I’ve simply used a Trigger:

<Label FontAttributes="Bold"
       TextColor="Gray" Text="{Binding CurrentUser.FullName}">
  <Label.Triggers>
    <DataTrigger TargetType="Label" Binding="{Binding CurrentUser.FullName}" Value=" ">
      <Setter Property="Text">Username</Setter>
    </DataTrigger>
  </Label.Triggers>
</Label>

As we’are using a Binding in the Binding property, we are free to use any converter needed to compute the value of our property. This is a bit more verbose but, at least, it works fine!

 

Happy coding!

Annoncing the Behaviors Toolkit for Xamarin!

February 2nd, 2016 | Posted by Tom in .NET | Xamarin - (0 Comments)

Since some days, I’ve started to work on a new project that, I hope, will help any Xamarin developers to improve their productivity.

Indeed, I’m very happy to announce that since today, the Behaviors Toolkit for Xamarin is now available !

image

What is it?

Currently in version 0.0.1, and as explained on the Github page, the Behaviors Toolkit for Xamarin is an easy-to-use means of adding common and reusable interactivity to your Xamarin applications with minimal code.

Why I’ve started to work on it?

Because I’m a lazy developer (or, at least, I’m a developer that like to reuse code) :)

Since I’ve been working on XAML applications, I’ve always enjoyed the behaviors feature which allow me to easily reuse the same feature on different controls. I’ve always thought it could be a good idea to get this in Xamarin apps because regardless the technology used: the needs are (often) the same.

Where to get it?

The source and documentation/code samples can be found in the Github repository:

https://github.com/ThomasLebrun/XamarinBehaviorsToolkit

If you want to access the package directly, you can get it from Nuget:

https://www.nuget.org/packages/Xamarin.Behaviors.Toolkit/

Want to share ideas/bugs?

I strongly encourage you to share your ideas and the bugs you’ve found. I’m always listening to goods ideas!

I also accept pulls requests that might be interesting so don’t hesitate to discuss about it too:

 

The project is still in its early stage but feel free to contact me on Twitter (@thomas_lebrun) to discuss about it!

 

Thanks and happy (Xamarin) coding!

Today, I wanted to try something new and to play a bit with sensors, on my Android device. After looking for an idea, I thought it could be interesting to implement a “ShakeDetector”, that means I wanted to be able to detect when the user shakes the device.

As I was a bit lazy, I first checked on Internet if nothing have been done and I just found different implementations of ShakeDetector for Android but written in Java, like this one: http://jasonmcreynolds.com/?p=388

Well, it does not matter: let’s see how we can adapt the Java code to get a clean C# code which allow us to detect the Shake gesture!

First step is to implement the ShakeDetector class, that will compute all the necessary stuff to detect the shake event, using the accelerometer. This class should implement SensorEventListener but this does not exists directly in Xamarin, we need to use the interface ISensorEventListener:

public class ShakeDetector : ISensorEventListener

But as soon as we try to implement the interface, we are ask to implement the following properties/methods:

image

If you try to implement them, you’ll see that they will never be called and so your code will never be called too. The reason and solution are described on this post by CheeseBaron: http://blog.ostebaronen.dk/2013/09/how-to-implement-intptr-handle-and.html

The brief explanation is that the Handle provides an Android Callable Wrapperto Android. All native Android interfaces extend IJavaObject so they will expect that to be implemented. So the simple answer is that you do not implement Handle or Disposeyourself, however you inherit from Java.Lang.Object which does that for you.

So to solve this error, you just need to modify the class ShakeDetector like this:

public class ShakeDetector : Java.Lang.Object, ISensorEventListener

Once this is done, we can continue to implement the rest of the class:

public class ShakeDetector : Java.Lang.Object, ISensorEventListener
{
    private const float SHAKE_THRESHOLD_GRAVITY = 2.7F;
    private const int SHAKE_SLOP_TIME_MS = 500;
    private const int SHAKE_COUNT_RESET_TIME_MS = 3000;

    private long mShakeTimestamp;
    private int mShakeCount;

    public delegate void OnshakeHandler(object sender, int shakeCount);
    public event OnshakeHandler Shaked;

    public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
    {
        // Do nothing in our case.
    }

    public void OnSensorChanged(SensorEvent e)
    {
        var x = e.Values[0];
        var y = e.Values[1];
        var z = e.Values[2];

        var gX = x / SensorManager.GravityEarth;
        var gY = y / SensorManager.GravityEarth;
        var gZ = z / SensorManager.GravityEarth;

        // gForce will be close to 1 when there is no movement.
        var gForce = Java.Lang.Math.Sqrt(gX * gX + gY * gY + gZ * gZ);

        if (!(gForce > SHAKE_THRESHOLD_GRAVITY))
            return;

        var now = JavaSystem.CurrentTimeMillis();

        // Ignore shake events too close to each other (500ms)
        if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now)
        {
            return;
        }

        // Reset the shake count after 3 seconds of no shakes
        if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now)
        {
            mShakeCount = 0;
        }

        mShakeTimestamp = now;
        mShakeCount++;

        if (this.Shaked != null)
        {
            this.Shaked(this, mShakeCount);
        }
    }
}

Note that I’ve adapt the code a bit to raise an event when the shake gesture is detected!

Now, we can continue to implement the code by registering/unregistering our new class (which implement ISensorEventListener) in the SensorManager:

[Activity(Label = "AndroidShakeDetector",
    MainLauncher = true,
    Icon = "@drawable/icon",
    ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : Activity
{
    private static readonly object _syncLock = new object();

    private SensorManager _sensorManager;
    private Sensor _sensor;
    private ShakeDetector _shakeDetector;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);

        var button = FindViewById<Button>(Resource.Id.MyButton);

        _sensorManager = (SensorManager)GetSystemService(Context.SensorService);
        _sensor = _sensorManager.GetDefaultSensor(SensorType.Accelerometer);

        _shakeDetector = new ShakeDetector();
        _shakeDetector.Shaked += (sender, shakeCount) =>
        {
            lock (_syncLock)
            {
                button.Text = shakeCount.ToString();
            }
        };
    }

    protected override void OnResume()
    {
        base.OnResume();

        _sensorManager.RegisterListener(_shakeDetector, _sensor, SensorDelay.Ui);
    }

    protected override void OnPause()
    {
        base.OnPause();

        _sensorManager.UnregisterListener(_shakeDetector);
    }
}

Be sure to allow access to the accelerometer and launch the application. As soon as you “shake” the device, you’ll see that the counter will increment and reset to 0 after 3 seconds of inactivity!

You can found the code on Github: https://github.com/ThomasLebrun/XamarinAndroidShakeDetector

 

Happy coding!

It’s always surprising to see how simples things doable in XAML can be a real pain when we try to do them using another language/technology.

This week, one of my customers using Xamarin asked me how to get a multilines label on iOS. After spending some time over Internet to search for a possible solution, I’ve ended with this one.

First, you need to calculate what will be the height of the label, according to the text you’ll have on it:

var labelSize = new NSString ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in sapien at tellus sollicitudin rutrum eu ut mi. Nunc vel cursus augue. Integer tempus sem orci, ac auctor quam bibendum non. Curabitur porttitor bibendum augue at auctor. In et cursus justo, eget finibus tortor. Donec fringilla magna dui, sed condimentum risus rhoncus in. In quis congue sem, quis elementum tellus. Cras quis orci ante.").
                    GetBoundingRect (
                        new CGSize (this.View.Frame.Width, float.MaxValue),
                        NSStringDrawingOptions.UsesLineFragmentOrigin,
                        new UIStringAttributes () { Font = UIFont.SystemFontOfSize (8) },
                        null);

As you can see, you have to specify the different properties that will be applied to the text (font size, etc.) but, most important, you have to specified the available space. Here, I indicate that the label will expand on all the width of the current view and will be able to have any height.

Once this is done, the variable labelSize will contains the desired size of the label so you’ll be able to set the height correctly:

var label = new UILabel (new CGRect(0, 0, this.View.Frame.Width, labelSize.Height))
{
    Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in sapien at tellus sollicitudin rutrum eu ut mi. Nunc vel cursus augue. Integer tempus sem orci, ac auctor quam bibendum non. Curabitur porttitor bibendum augue at auctor. In et cursus justo, eget finibus tortor. Donec fringilla magna dui, sed condimentum risus rhoncus in. In quis congue sem, quis elementum tellus. Cras quis orci ante.",
    Font = UIFont.SystemFontOfSize (8),
    Lines = 0,
    LineBreakMode = UILineBreakMode.WordWrap
};

this.Add(label);

Here, I simply add a label to the location 0, 0 and I specified the frame width for the width and I use the labelSize.Height property to set the height.

Don’t forget to set the properties Lines to 0 and LineBreakMode to WordWrap! Indeed, set 0 to the Lines property allows the label to have an unlimited number of lines and the LineBreakMode property tells it how to break the lines (it can wrap words, characters, etc.)

Also, each text properties (font size, typeface, etc.) need to be the same as the one you used to calculate the size of the label.

 

Happy coding!

Couchbase Lite is small, light, document-oriented (No-SQL) JSON database engine available that can be easily used in any Xamarin (iOS / Android) applications, thanks to the dedicated component: https://components.xamarin.com/view/couchbase-lite-net

 

Using Couchbase Lite is really easy and consists in only a few steps. First, we need to create a database:

var database = Manager.SharedInstance.GetDatabase("Database");

Once the database is created, we can add some documents to it (a document is something similar to a table). Each document is composed of a set of properties:

// Create a document
var properties = new Dictionary<string, object>()
{
    { "Id", Guid.NewGuid() },
    { "Title", "Getting started with Couchbase Lite!" },
    { "CreationDate", DateTime.Now },
};

var document = database.CreateDocument();
document.PutProperties(properties);

Of course, later in your application, you’ll be able to retrieve a specific document (via its Id property) to enumerate all its properties:

// Retrieve a document
var alert = new AlertDialog.Builder(this);
alert.SetTitle("CouchBase Lite!");
alert.SetPositiveButton("OK", (sender, args) => {});

var retrievedDocument = database.GetDocument(document.Id);
foreach (var property in retrievedDocument.Properties)
{
    alert.SetMessage(string.Format("Property Name: {0} - Property Value: {1}", property.Key, property.Value));
    alert.Show();
}

A document can, of course, be updated with new properties. The best way to update the document is to get its properties and update the interesting one:

// Update a document
var updatedProperties = new Dictionary<string, object>(retrievedDocument.Properties);
updatedProperties["Title"] = "This is an updated title!";

var updatedDocument = retrievedDocument.PutProperties(updatedProperties);

To delete a document (or a database), simply use the dedicated methods on the objects:

// Delete a document
updatedDocument.DeleteDocument();
database.Delete();

 

As I told you previously, Couchbase Lite is very powerful but very simple to use and to add to your application, don’t hesitate to give it a try!

 

Happy coding!

Recently, I’ve been asked by one of my customer how it could be possible, in a Xamarin.Android application, to send an email with an attachment.

After searching over Internet, I’ve managed to produce the following method:

var downloadsFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
var filePath = Path.Combine(downloadsFolder.Path, "Demo.txt");

var createdFile = File.CreateText(filePath);
await createdFile.WriteAsync("Hello World!");
await createdFile.FlushAsync();

var file = new Java.IO.File(filePath);
file.SetReadable(true, false);

var uri = Android.Net.Uri.FromFile(file);

var email = new Intent(Intent.ActionSend);
email.PutExtra(Intent.ExtraEmail, new[] { "tom@email.com" });
email.PutExtra(Intent.ExtraSubject, "Sample email with attachment");
email.PutExtra(Intent.ExtraStream, uri);

email.SetType("message/rfc822");

this.StartActivity(Intent.CreateChooser(email, "Email"));

The most important part here is the beginning of the method. Indeed, to be able to add the attachment, it needs to be accessible to the app that will send the email. So we need to create a copy of our file in a location that can be accessible by any app so, in our case, we choose the Downloads folder.

Another interesting point is the use of the SetReadable method to indicate that the file need to be readable but to anyone and not only the file’s creator!

Finally, depending of where you create the copy, you might set the right permissions to your app:

image

Also, once your application is back to forward, don’t forget to delete the copy to clean up the Downloads folder!

 

Happy coding! :)

First of all, let me be clear: I love MVVM Light Framework, specially in Windows applications. When I started to look at Xamarin, I was very enthusiast to see that Laurent ported its Framework on that platform.

But I have to say that I was a bit disappointed when I saw it was limited (who said “light” ;)) in terms of features. One of the feature found in others MVVM Frameworks (for example in MVVM Cross) that I particularly like is the XML attributes to apply data binding.

So I’ve decided to create a small proof of concept of how you can have, in a Xamarin.Android application, a data binding engine that supports the following points:

  • Looks like the same data binding that the one we can found in XAML apps
  • Support OneWay/TwoWay bindings
  • Support commands
  • Support converters
  • Support method calls when events are raised
  • and, the last (but not the least), support data binding through XML attributes

Please, remind that this is, for now, most a prototype than something useable in your applications. In fact, all the code here works fine but I’ve not checked for memory performances, I should have used weak events, I’ve not implemented cache, etc.

All the code will be attached to the post so I’ll just provide you some pointers to the interesting parts.

At first, we need a base class for all our ViewModels:

public class BindableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Of course, this version is pretty simple and you can easily improve it. Once we have that base class, we’ll define a base activity for all the views of our application:

public abstract class BindingActivity<TViewModel> : Activity where TViewModel : BindableObject
{
    public TViewModel DataContext { get; set; }

    public int ViewLayoutResourceId { get; set; }

    protected BindingActivity(int viewLayoutResourceId)
    {
        this.ViewLayoutResourceId = viewLayoutResourceId;

        this.DataContext = ViewModelFactory.Create<TViewModel>();
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        this.SetContentView(this.ViewLayoutResourceId);

        BindingEngine.Initialize(this);
    }
}

As you can see, the BindingActivity expose a DataContext property (as we found in XAML) and initialize the binding engine. To use it, you just have to inherit from that class and pass the type of ViewMode:

[Activity(Label = "SampleBindingEngine", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : BindingActivity<MainViewModel>
{
    int _count = 1;

    public MainActivity()
        : base(Resource.Layout.Main)
    {
    }
}

The ViewModel is composed by the following elements:

public class MainViewModel : BindableObject
{
    private bool _isBusy;

    public bool IsBusy
    {
        get { return _isBusy; }
        set
        {
            _isBusy = value;

            this.OnPropertyChanged();
        }
    }

    private string _sampleText;
    public string SampleText
    {
        get { return _sampleText; }
        set
        {
            _sampleText = value;

            this.OnPropertyChanged();

            if (this.ButtonClickCommand != null)
            {
                this.ButtonClickCommand.RaiseCanExecuteChanged();
            }
        }
    }

    public RelayCommand ButtonClickCommand { get; set; }

    public MainViewModel()
    {
        this.SampleText = "Hey, Xamarin World!";

        this.ButtonClickCommand = new RelayCommand(o =>
        {
            this.SampleText = "Text Changed from First Command!";
        },
        o => !string.IsNullOrWhiteSpace(this.SampleText));
    }

    private void ClickOnButton()
    {
        this.SampleText = "Text Changed from Method!";
    }
}

This is on that class, BindingEngine, that most of the job is done:

private const string ViewLayoutResourceIdPropertyName = "ViewLayoutResourceId";
public static void Initialize<TViewModel>(BindingActivity<TViewModel> bindingActivity) where TViewModel : BindableObject
{
    // TODO
}

First, we need to get the XML elements that composed the view;

List<XElement> xmlElements = null;

// Find the value of the ViewLayoutResourceId property
var viewLayoutResourceIdProperty = bindingActivity.GetType().GetProperty(ViewLayoutResourceIdPropertyName);
var viewLayoutResourceId = (int)viewLayoutResourceIdProperty.GetValue(bindingActivity);

if (viewLayoutResourceId > -1)
{
    // Load the XML elements of the view
    xmlElements = GetViewAsXmlElements(bindingActivity, viewLayoutResourceId);
}

/// <summary>
/// Returns the current view (activity) as a list of XML element.
/// </summary>
/// <typeparam name="TViewModel">The type of the ViewModel associated to the activity.</typeparam>
/// <param name="bindingActivity">The current activity we want to get as a list of XML elements.</param>
/// <param name="viewLayoutResourceId">The id corresponding to the layout.</param>
/// <returns>A list of XML elements which represent the XML layout of the view.</returns>
private static List<XElement> GetViewAsXmlElements<TViewModel>(BindingActivity<TViewModel> bindingActivity, int viewLayoutResourceId) where TViewModel : BindableObject
{
    List<XElement> xmlElements;

    using (var viewAsXmlReader = bindingActivity.Resources.GetLayout(viewLayoutResourceId))
    {
        using (var sb = new StringBuilder())
        {
            while (viewAsXmlReader.Read())
            {
                sb.Append(viewAsXmlReader.ReadOuterXml());
            }

            var viewAsXDocument = XDocument.Parse(sb.ToString());
            xmlElements = viewAsXDocument.Descendants().ToList();
        }
    }

    return xmlElements;
}

Then, we’ll check if there are some XML elements that contains our “Binding” attribute and, if that’s true, we’ll get the current view a a list of .NET objects:

private static readonly XName BindingOperationXmlNamespace = XNamespace.Get("http://schemas.android.com/apk/res-auto") + "Binding";

// If there is at least one 'Binding' attribute set in the XML file, get the view as objects
if (xmlElements != null && xmlElements.Any(xe => xe.Attribute(BindingOperationXmlNamespace) != null))
{
    viewElements = GetViewAsObjects(bindingActivity);
}

/// <summary>
/// Returns the current view (activity) as a list of .NET objects.
/// </summary>
/// <typeparam name="TViewModel">The type of the ViewModel associated to the activity.</typeparam>
/// <param name="bindingActivity">The current activity we want to get as a list of XML elements.</param>
/// <returns>A list of .NET objects which composed the view.</returns>
private static List<View> GetViewAsObjects<TViewModel>(BindingActivity<TViewModel> bindingActivity) where TViewModel : BindableObject
{
    // Get the objects on the view
    var rootView = bindingActivity.Window.DecorView.FindViewById(Resource.Id.Content);

    return GetAllChildrenInView(rootView, true);
}

/// <summary>
/// Recursive method which returns the list of children contains in a view.
/// </summary>
/// <param name="rootView">The root/start view from which the analysis is performed.</param>
/// <param name="isTopRootView">True is the current root element is, in fact, the top view.</param>
/// <returns>A list containing all the views with their childrens.</returns>
private static List<View> GetAllChildrenInView(View rootView, bool isTopRootView = false)
{
    if (!(rootView is ViewGroup))
    {
        return new List<View> { rootView };
    }

    var childrens = new List<View>();

    var viewGroup = (ViewGroup)rootView;

    for (int i = 0; i < viewGroup.ChildCount; i++)
    {
        var child = viewGroup.GetChildAt(i);

        var childList = new List<View>();
        if (isTopRootView)
        {
            childList.Add(child);
        }

        childList.AddRange(GetAllChildrenInView(child));

        childrens.AddRange(childList);
    }

    return childrens;
}

Now, we’ll extract all the binding operations implemented in the XML file:

if (xmlElements != null && xmlElements.Any() && viewElements != null && viewElements.Any())
{
    // Get all the binding operations inside the XML file.
    var bindingOperations = ExtractBindingOperationsFromLayoutFile(xmlElements, viewElements);
    if (bindingOperations != null && bindingOperations.Any())
    {
        // Find the value of the DataContext property (which is, in fact, our ViewModel)
        var viewModel = bindingActivity.DataContext as BindableObject;
        if (viewModel != null)
        {
            // TODO
        }
    }
}

/// <summary>
/// Extract the Binding operations (represent by the Binding="" attribute in the XML file).
/// </summary>
/// <param name="xmlElements">The list of XML elements from which we want to extract the Binding operations.</param>
/// <param name="viewElements">The list of .NET objects corresponding to the elements of the view.</param>
/// <returns>A list containing all the binding operations (matching between the Source property, the Target property, the Control bound to the .NET property and the Mode of the binding).</returns>
private static List<BindingOperation> ExtractBindingOperationsFromLayoutFile(List<XElement> xmlElements, List<View> viewElements)
{
    var bindingOperations = new List<BindingOperation>();

    for (int i = 0; i < xmlElements.Count; i++)
    {
        var currentXmlElement = xmlElements.ElementAt(i);

        if (currentXmlElement.Attributes(BindingOperationXmlNamespace).Any())
        {
            var xmlBindings = currentXmlElement.Attributes(BindingOperationXmlNamespace);

            foreach (var xmlBindingAttribute in xmlBindings)
            {

                var xmlBindingValue = xmlBindingAttribute.Value;

                if (!xmlBindingValue.StartsWith("{") || !xmlBindingValue.EndsWith("}"))
                {
                    throw new InvalidOperationException(string.Format("The following XML binding operation is not well formatted, it should start with '{{' and end with '}}:'{0}{1}", Environment.NewLine, xmlBindingValue));
                }

                var xmlBindingOperations = xmlBindingValue.Split(';');

                foreach (var bindingOperation in xmlBindingOperations)
                {
                    if (!bindingOperation.Contains(","))
                    {
                        throw new InvalidOperationException(string.Format("The following XML binding operation is not well formatted, it should contains at least one ',' between Source and Target:{0}{1}", Environment.NewLine, xmlBindingValue));
                    }

                    var bindingSourceValueRegex = new Regex(@"Source=(\w+)");
                    var bindingSourceValue = bindingSourceValueRegex.Match(bindingOperation).Groups[1].Value;

                    var bindingTargetValueRegex = new Regex(@"Target=(\w+)");
                    var bindingTargetValue = bindingTargetValueRegex.Match(bindingOperation).Groups[1].Value;

                    var bindingConverterValueRegex = new Regex(@"Converter=(\w+)");
                    var bindingConverterValue = bindingConverterValueRegex.Match(bindingOperation).Groups[1].Value;

                    // Converter parameter support using more than just a word.
                    var bindingConverterParameterValueRegex = new Regex(@"ConverterParameter='(\w+\s(.\w+)+)");
                    var bindingConverterParameterValue = bindingConverterParameterValueRegex.Match(bindingOperation).Groups[1].Value;

                    var bindingModeValue = BindingMode.OneWay;

                    var bindingModeValueRegex = new Regex(@"Mode=(\w+)");
                    var bindingModeValueRegexMatch = bindingModeValueRegex.Match(bindingOperation);

                    if (bindingModeValueRegexMatch.Success)
                    {
                        if (!System.Enum.TryParse(bindingModeValueRegexMatch.Groups[1].Value, true, out bindingModeValue))
                        {
                            throw new InvalidOperationException(string.Format("The Mode property of the following XML binding operation is not well formatted, it should be 'OneWay' or 'TwoWay':{0}{1}", Environment.NewLine, xmlBindingValue));
                        }
                    }

                    bindingOperations.Add(new BindingOperation { Control = viewElements.ElementAt(i), Source = bindingSourceValue, Target = bindingTargetValue, Converter = bindingConverterValue, ConverterParameter = bindingConverterParameterValue, Mode = bindingModeValue });
                }

            }
        }
    }

    return bindingOperations;
}

And then, we just need to analyse each binding operation to get the Mode, Source and Target propertied so we can setup the binding. For example, here is how we can setup a binding between a command (or a method) and an event:

var sourceProperty = typeof(TViewModel).GetProperty(bindingOperation.Source);

var bindingEvent = bindingOperation.Control.GetType().GetEvent(bindingOperation.Target);
if (bindingEvent != null)
{
    // The target is an event of the control

    if (sourceProperty != null)
    {
        // We need to ensure that the bound property implements the interface ICommand so we can call the "Execute" method
        var command = sourceProperty.GetValue(viewModel) as ICommand;
        if (command == null)
        {
            throw new InvalidOperationException(string.Format("The source property {0}, bound to the event {1}, needs to implement the interface ICommand.", bindingOperation.Source, bindingEvent.Name));
        }

        // Add an event handler to the specified event to execute the command when event is raised
        var executeMethodInfo = typeof(ICommand).GetMethod("Execute", new[] { typeof(object) });

        AddHandler(bindingOperation.Control, bindingOperation.Target, () =>
        {
            if (!_preventUpdateForSourceProperty)
            {
                executeMethodInfo.Invoke(command, new object[] { null });
            }
        });

        // Add an event handler to manage the CanExecuteChanged event of the command (so we can disable/enable the control attached to the command)
        var currentControl = bindingOperation.Control;

        var enabledProperty = currentControl.GetType().GetProperty("Enabled");
        if (enabledProperty != null)
        {
            enabledProperty.SetValue(currentControl, command.CanExecute(null));

            AddHandler(command, "CanExecuteChanged", () => enabledProperty.SetValue(currentControl, command.CanExecute(null)));
        }
    }
    else
    {
        // If the Source property of the ViewModel is not a 'real' property, check if it's a method
        var sourceMethod = typeof(TViewModel).GetMethod(bindingOperation.Source, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
        if (sourceMethod != null)
        {
            if (sourceMethod.GetParameters().Length > 0)
            {
                // We only support calls to methods without parameters
                throw new InvalidOperationException(string.Format("Method {0} should not have any parameters to be called when event {1} is raised.", sourceMethod.Name, bindingEvent.Name));
            }

            // If it's a method, add a event handler to the specified event to execute the method when event is raised
            AddHandler(bindingOperation.Control, bindingOperation.Target, () =>
            {
                if (!_preventUpdateForSourceProperty)
                {
                    sourceMethod.Invoke(viewModel, null);
                }
            });
        }
        else
        {
            throw new InvalidOperationException(string.Format("No property or event named {0} found to bint it to the event {1}.", bindingOperation.Source, bindingEvent.Name));
        }
    }
}

Using the binding feature from an AXML file is as simple as adding a new XML attribute matching the source property to the target property. Here are a few example of what is supported for now:

<!-- Simple OneWay binding -->
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello"
        local:Binding="{Source=SampleText, Target=Text}" />
<!-- Simple TwoWay binding -->
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello"
        local:Binding="{Source=SampleText, Target=Text, Mode=TwoWay}" />
<!-- Binding an event to a command -->
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Change EditText From Command"
        local:Binding="{Source=ButtonClickCommand, Target=Click}" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello"
        local:Binding="{Source=SampleText2, Target=Text, Mode=TwoWay;Source=TextCommand, Target=TextChanged}" />
<!-- Binding an event to a method -->
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Change EditText From Method"
        local:Binding="{Source=ClickOnButton, Target=Click}" />
<!-- Binding with a converter -->
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello"
        local:Binding="{Source=SampleBool, Target=Text, Mode=TwoWay, Converter=BooleanToStringConverter}" />
<!-- Binding with a converter & converter parameter -->
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello"
        local:Binding="{Source=SampleBool, Target=Text, Mode=TwoWay, Converter=BooleanToStringConverter, ConverterParameter='You can put any string here'}" />

I highly recommend you to take a look at the source code to see what can be done, how to setup TwoWay binding, etc. Also, for now, the engine does not support nested source properties (MyObject.MyFirstProperty.MySecondProperty) but this is something that I plan to implement as soon as possible (and if anybody think it’s a good idea/engine)!

 

The source code is available here for now but, based on feedbacks I have, it’s possible that I put it on Github to continue the development (and to allow pull requests).

So guys, what do you think about it ? Should I continue developing this binding engine ? Feel free to keep in touch with me by email, Twitter or in the comments to discuss about it!

 

Happy coding!

Scanning QRCodes (or barcodes) in a Xamarin.Forms application is really easy thanks to a well-know project ZXing, that has been ported to Xamarin (and for all the devices: iOS, Android and Windows Phone).

 

To implement the feature, first add a reference to the ZXing component to your projects. Then, as always, we need to find a way to communicate between the Forms project (that contains the logic) and the various UI projects. To solve this, we’ll used the DependencyService to register an interface and create the various implementations.

The interface is pretty simple:

public interface IQrCodeScanningService
{
    Task<string> ScanAsync();
}

And here is the implementation for the Windows Phone project:

public class QrCodeScanningService : IQrCodeScanningService
{
    public async Task<string> ScanAsync()
    {
        var scanner = new ZXing.Mobile.MobileBarcodeScanner(App.RootFrame.Dispatcher);
        var scanResults = await scanner.Scan();

        return scanResults.Text;
    }
}

And now, let’s take a look to the implementation for the Android project (of course, it’s also possible to create an iOS implementation):

public class QrCodeScanningService : IQrCodeScanningService
{
    public async Task<string> ScanAsync()
    {
        var scanner = new ZXing.Mobile.MobileBarcodeScanner(Application.Context);
        var scanResults = await scanner.Scan();

        return scanResults.Text;
    }
}

As you can see, each version is particular (the constructor of the MobileBarcodeScanner takes a different parameters, according to the platform).

For each implementation, don’t forget to add the Dependency attribute so the DependencyService can register the interface and the implementation:

// Windows Phone
[assembly: Dependency (typeof(QrCodeScanningService))]
namespace QrCodeScanningWithXamarin.WinPhone.Helpers

// Android
[assembly: Dependency(typeof(QrCodeScanningService))]
namespace QrCodeScanningWithXamarin.Droid.Helpers

And now, in the Forms project (Shared or portable), we can use the DependencyService to retrieve the instance of the class that implement our interface:

var scanButton = new Button
{
    Text = "Launch scan"
};
scanButton.Clicked += async (sender, args) =>
{
    var url = await DependencyService.Get<IQrCodeScanningService>().ScanAsync();

    Device.OpenUri(new Uri(url));
};

Launch your application and click on the button to display the overlay allowing the user to scan a QRCode or Barcode. Once the view appears on the screen, click on it and, after the autofocus is performed, the scan is done and the results are returned so you can use them (display them, launch the URL, etc.)

 

Happy coding!