Tuesday, March 31, 2015

The Linker - Mono's virtual 400 HP code chainsaw

One behind the scenes tool that most Xamarin newbies don't know nothing about is the linker. The linker gets called during the build of your assemblies. It has a single purpose and that is to reduce the size of your assembly. So how does it do that you say! It does it by firing up a digital, virtual, 400-HP chainsaw and cuts away the parts that your code doesn't use.

GREAT! How do I enable it?!

For iOS and Android the linker is enabled by default for projects that targets actual devices and disabled if you target emulators/simulators. The reason for this is to reduce build time when deploying to a simulator.

You can edit the linker settings under project properties; iOS build for iOS and Android options for Android. 

Anything else I should know

Yes, there are three levels of linking;
  • Link all assemblies which means that all code is subject for linking
  • Link SDK assemblies only which means that only Xamarin Core assemblies will be linked. Default for deploy to actual devices.
  • Dont link which means, well, don't link... Default for deploy to simulators/emulators.

Outstanding, why don't I use Link all all the time then!?

The first reason is that deploy time increases since linking takes time. So when deploying to the simulator or for your device while testing, it simply is not worth the extra time.

The other more important reason that should've been first is that the linker can be slightly evil. It can remove stuff that you meant to keep. Linking is carried out through static analysis of the code, so any classes that are instantiated through reflection and sometimes through IoC will not be detected and so they will be cut away. You can save the day by using the [Preserve] attribute to decorate classes and tell them to hide from the linker. If you're coding in a PCL that doesn't have the PreserveAttribute references you can just roll your own. Simply call it "PreserveAttribute" and the linker will see it. Think of it as garlic for linker vampires...

The third reason not to use link all is that this might affect any third party libraries that you have referenced that isn't "linker friendly".

So what's the summary of all this

Simply leave the linker as is and carry on with your life. Nothing to see here, circulate!

References


Monday, March 30, 2015

Connecting to Android Player using VS and Parallels

This is a short guide for how to connect to Android Player in case you're using Visual Studio in Windows through Parallels. I use to do this in a way more complicated manor before I realized that it's just this simple. Looking ahead, In VS 2015, Microsofts gives us an x86/hyper-V Android emulator that looks great. But for now, this works the best.

Start your engines

Fire up Android Player (or any other emulator of your choice that runs Android) in OS X and get the address to the emulator. Click on the settings cog and note the IP Address.

Connect to the emulator

In Windows, open your project in Visual Studio and hit Tools -> Android -> Android Adb Command Prompt. Write adb connect [the IP address] and you should then be connected to your emulator like in the image below.


Run your project

You should now see your device in Visual Studio!


Troubleshooting

Of course, things can go wrong. The issues I've encountered are these.

1) I had to disable my wireless network while connected to the local wired network. Surely this is a configuration issue that I just haven't bothered with yet.

2) The emulator doesn't show up. Restart Visual Studio.

3) You have to reconnect each time your Mac goes to sleep...

4) Firewalls... Make sure port 5555 is open for TCP from Windows to OS X.

Sunday, March 22, 2015

Navigation from a ListView

The problem to be solved

One drawback with the vanilla ListView that comes with Xamarin.Forms is that once an item is selected it can't be selected again until you select something else and reselect it. Usually this is fine, unless you use the ListView to navigate away to a new page. When you return you cannot re-navigate to the same page again.

The solution

The solution to this is simple, just hook up to the ItemSelected event for the ListView and set the SelectedItem property to null.

So the short version of you quick-googlers would be the following line of code. (in the View)

  // Reset the selected item to make it selectable again
  duckListView.ItemSelected += (s, e) => {
    duckListView.SelectedItem = null; 


And the navigation should be done in the ViewModel

        public Duck SelectedDuck
        {
            set 
            {
                if (value != null)
                {
                    // IoC omitted, we should really get someone else to 
                    // create these objects for us.
                    var viewModel = new DuckViewModel() { Duck = value };
                    var page = new DuckView(viewModel);
                    _navigation.PushAsync(page);
                }
            }
        }

The more verbose version

You could also navigate directly from this event handler, but you should feel it deep in your heart that that is just wrong. Instead we handle navigation in the ViewModel. I've created a sample project to do this. Also, I'm doing this without any additional framework that would handle navigation for you so that's why I need to provide my ViewModel with a navigation interface.

I'll present each file to you below or just download the sample solution from here.

The Model

Our model is simple. It's just a duck with a name... As you might recall, a model could be anything really. In this case it's a simple class.
    
    public class Duck
    {
        public string Name
        {
            get;
            set;
        }
    }

The ViewModel (s)

We've got two ViewModels, but it's really only the MainViewModel that's interesting. It initializes it's own data, which usually should be done async from another source. It doesn't implement INotifyPropertyChanged either, as is should but for this sample it's good enough.

What to focus on is the SelectedDuck property that handles the navigation. We only implement a setter for this since we reset the selected item anyhow in the view itself and on top of that navigate away from the page.


    /// <summary>
    /// The sample ViewModel. Should implement INotifyPropertyChanged
    /// </summary>
    public class MainViewModel
    {
        private INavigation _navigation;

        public MainViewModel(INavigation navigation)
        {
            _navigation = navigation;

            Ducks = new List<Duck>()
            {
                new Duck() { Name = "George" },
                new Duck() { Name = "Bob" },
                new Duck() { Name = "Sarah" },
                new Duck() { Name = "Clint" },
            };
        }

        /// <summary>
        /// A list of ducks
        /// </summary>
        /// <value>The ducks.</value>
        public List<Duck> Ducks
        {
            get;
            set;
        }

        public Duck SelectedDuck
        {
            set 
            {
                if (value != null)
                {
                    // IoC omitted, we should really get someone else to 
                    // create these objects for us.
                    var viewModel = new DuckViewModel() { Duck = value };
                    var page = new DuckView(viewModel);
                    _navigation.PushAsync(page);
                }
            }
        }
    }


The other ViewModel (DuckViewModel) simply references the selected duck on the page we navigate to.

   public class DuckViewModel
    {
        public DuckViewModel()
        {
        }

        public Duck Duck
        {
            get;
            set;
        }
    }



The View

That leaves us with the view that consists of two parts; XAML and the code behind. Usually you don't want any code expect the ViewModel-binding in the code-behind since it's very platform coupled, but in this case we need to do an exception. We need to reset the SelectedItem property of the ListView.

    public partial class MainView : ContentPage
    {
        public MainView()
        {
            InitializeComponent();
            BindingContext = new MainViewModel(this.Navigation); // Should be injected

            // Reset the selected item to make it selectable again
            duckListView.ItemSelected += (s, e) => {
                duckListView.SelectedItem = null; 
            };
        }
    }

The XAML parts look like this.

xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ListViewNavigation.MainView">
    <ContentPage.Content>

      <ListView x:Name="duckListView"
             IsGroupingEnabled="false"
             ItemsSource="{Binding Ducks}"
             HasUnevenRows="true"
             SelectedItem="{Binding SelectedDuck}">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
            <Label Font="Large" Text="{Binding Name}" />
        </ViewCell>
      </DataTemplate>
     </ListView.ItemTemplate>
    </ListView>


    </ContentPage.Content>
</ContentPage>

Summary

There is a lot missing in forms of Ioc and base frameworks for MVVM (like MvvmLight och MvvmCross). Is there an alternative for the extreme mvvm-purists? Yes, you could hook up a GestureRecognizer on each item in the ListView but some platform specific animations will be lost if you do so.

Please give feedback what ever you feel like! And if there's a better way, I would love for you to enlighten me! :D

Monday, March 16, 2015

URLEncode in Xamarin Forms PCL

The shortest blog post ever. If you want to URLEncode (or decode) in a Xamarin Forms PCL, simply use:

var encoded = System.Net.WebUtility.UrlEncode(veryUnEncoded);

There is also methods for HTML encoding and decoding in there and url encoding/decoding to bytes. I always have to look this up for some reason, that's why I'm putting it on my blog so that I can find it easily.

Thank you, that is all...