MFractor – Productivity tools for Xamarin Studio

MFractor is a developer productivity tool, for Xamarin Studio on the Mac, that helps you write and discover errors in code and XAML, for your Xamarin Forms application, much faster. It can be considered similar to ReSharper, but for Xamarin Forms. You will receive suggestions and actions in real-time, while writing your app in Xamarin Studio. It categorizes these into 4 types, Refactoring, Generation, Organise and Code Issue Fixes.

Setup And Install

Go to MFractor and download the latest version on your Mac. Open up Xamarin Studio and go to Add-ins.

Then Install From File and find the download. Then press Open.

After install it will ask to activate. Close the Add-in manager first, then enter your email and activate. Now you are ready to use MFractor.

Unused Namespaces

When you first load a XAML page, with MFractor installed, you will notice an underline on the local xmlns attribute. Hover over this, and you will see a notice. This one is mentioning that you aren’t using the prefix.

You can delete it, or Mfractor can remove it for you.

Note: Any action can be done via MFractor, via Right Click or pressing Alt + Return.

ViewModel

Now, before MFractor gets really smart, you need to ensure your pages are wired up to a ViewModel, by either the static locator pattern or naming convention. First, lets create a ViewModel called MainViewModel.cs.

public class MainViewModel
{

}

If you are wiring up your VIewModel with the static locator pattern, create a ViewModelLocator class.

public static class ViewModelLocator 
{
    public static readonly MainViewModel MainViewModel = new MainViewModel(); 
}

Then set the BindingContext in the page.

BindingContext="{x:Static local:ViewModelLocator.MainViewModel}"

The other way, which is the preferred way, is by naming convention. This is easier and allows it to work when you are using other MVVM frameworks. If the prefix of your Page and ViewModel are the same, it will assume they are the connected. E.g. MainPage will connect to MainViewModel.

Property Value

Now lets change the label to bind to a property in the ViewModel, call TestValue.

<Label Text="{Binding TestValue}" />

When you hover over TestValue, it will let you know this property doesn’t exist in the ViewModel.

To generate the property, right click or Alt + Return, and press Generate property name TestValue in MainViewModel.

 

It will now take you to your ViewModel, where you can decide to place the property. It will then create a property as shown below.

public class MainViewModel
{
    public string TestValue { get; set; }
}

Converters

MFractor can even detect if a property isn’t the same type it is bound to, and can detect and understand converters. Let’s say we bind IsVisible to the same property.

<Label Text="{Binding TestValue}" IsVisible="{Binding TestValue}" />

Now you will see the following hint. To resolve this we need to create a converter.

To add proper support for Converters we need to add a little attribute first. Add the below into your project.

[ValueConversion(typeof(string), typeof(bool))]
public class TextToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !string.IsNullOrEmpty((string)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Then create the converter, TextToBoolConverter. The only difference here is you have to add the attribute to the top of the class to define the conversion.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ValueConversionAttribute : Attribute
 {
 public ValueConversionAttribute(Type input, Type output)
 {
 }

 public Type ParameterType { get; set; }
 }

Then add the converter to your page and add in the converter to the IsVisible binding.

<ContentPage.Resources>
    <ResourceDictionary>
        <local:TextToBoolConverter x:Key="TextToBoolConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

<Label Text="{Binding TestValue}" IsVisible="{Binding TestValue, Converter={StaticResource TextToBoolConverter}}" />

The warning will now go away. If you change the ValueConversion attribute to return a different type, it will now give a warning.

More Information

I have only scratched the surface of MFractor and there are plenty more features and functionality. Check out the docs for everything you can do, or have a look at Matthew Robbins on the Channel9 show with James Montemagno, called MFractor – Incredible Tools for Xamarin, for a video walkthrough.

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer |
Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

Leave A Comment?