How To,Tutorial,Wintellect Blogs

Xamarin Forms for Windows Developers: Tips, Tricks and Lessons Learned, Part 2

4 May , 2017  

xamarin forms best practices

The Recap

This series of posts is devoted to all the things I wish I knew, as a long-time Microsoft Enterprise developer, before I provisioned my first xPlatform solution: The 60+ tips, tricks, and lessons learned I’ve accumulated over the last couple of years using Xamarin Forms (in Visual Studio for Windows.)

Many of these things are things an Android or iOS developer (i.e.; college student) probably already knows, but not a Microsoft developer. Some of these things will seem ridiculous to even share, and some so obvious that perhaps some developers will pretend I am the only one who didn’t already know about it. Some of these things will be super-extra important, and some will be meaningless to almost everyone. If you “lean native” you might even argue with me about some of them. That being said, here is the second batch of TTLL:

The Bits

If you’re not using Visual Studio Mobile Center you are totally missing out.

If you haven’t heard of Visual Studio Mobile Center it’s understandable. Have you heard of HockeyApp? If you haven’t heard of HockeyApp, have you heard of a Sony Walkman? Visual Studio Mobile Center is one of the coolest (and most under-marketed) “almost” brand new offerings, from Microsoft for managing mobile app development and development lifecycles. (Mostly) based off the original HockeyApp services, Mobile Center provides a bunch of services needed by mobile developers, into a single, integrated product. Imagine having a service that will automatically build, test, and then distribute your app, every time you commit a build to GitHub or Team Services. Imagine getting full stack traces and analytics for everything that happens with your app and users. Imagine having your app tested on over 400 devices before it goes live, while you binge watch Gilmore Girls. Stop imaging. Start using Visual Studio Mobile Center.

xamarin forms best practices

A realistic example of a developer using Visual Studio Mobile Center

The Xamarin.Forms.Device.StartTimer is a GREAT timer.

Even though Xamarin Forms is the wrong platform for graphic-intensive gaming, there are a number of scenarios where you just plain need a reliable timer. One example I use all the time is to lock a user out after periods of inactivity. The Device.StartTimer starts a recurring timer using the device clock capabilities, and is simple to use, easily supports async, and is ultra-reliable. Don’t kill yourself with trying to figure out ways to use the old Timer object, or even Reactive. Just use Device.StartTimer.

The ever-reliable Device.StartTimer in Xamarin Forms

Use MVVM and DON’T create your UI in code-behind.

So many examples of using Xamarin Forms, especially for creating controls, spin up and spit out StackPanels and Labels, and all kinds of junk via code-behind. Don’t do this. I mean, if you want to do it for fun, just to prove you can, do it. In fact, do it three times, and then never do it again. Xamarin Forms development is XAML-based. Did you know that’s where the Xam in Xamarin comes from? Just like WPF and UWP, XAML-based development is designed to be used with Model-View-ViewModel (MVVM) patterns. This means models, views (pages) and view models. This also means using bindings, converters, and commanding. All of these concepts get wrecked up, or ridiculous to manage, unless you create your UI with XAML, and MVVM for your logic patterns.

Discover HasUnevenRows and SeparatorVisibility for ListViews.

If you’re new to the Xamarin Forms ListView (which is great, BTW) you may be having challenges getting the vertical layout to dynamically “grow” the way you’d expect, and may notice (especially on some versions of iOS) you have ugly horizontal lines magically appear as dividers between items. Two properties you might not be aware of (because they don’t exist in WPF or UWP-based ListView controls) are HasUnevenRows and SeparatorVisibility. Setting these properties to False and None, respectively, is the equivalent of typing WorkRightAndLookRight = true.

If you are using AbsoluteLayout a lot, you’re probably using the wrong platform.

I know, you are a developer, which means you have a huge ego, a paradoxically low feeling of self worth, and like to be in control. Yes, this is true, and the pun is intended. Just because Xamarin Forms has an AbsoluteLayout, doesn’t mean you should use it. Using AbsoluteLayout is messy and support for layout properties changes with the wind. There are other, better ways to do what you want to do. Really.

Examples of layouts that are NOT good fits for Xamarin Forms.

Almost every single Visual Studio Emulator for Android issue comes down to something missing in the Android SDK.

Yes, there are rare exceptions, like using Windows 10 Home and not having Hyper-V support. (What you are doing running Windows 10 Home I won’t ask, but it could be worse, you could be using a Mac*.) However, in general, Android needs a lot of junk in its trunk, and therefore Xamarin Forms needs all that junk to run and compile on Android devices.

Although I will be devoting an entire post to this sensitive, and almost religious topic, always go to Tools > Android > Android SDK Manager… and install everything recent, like API 19+. Now if you are super-smart in the Android development world you are going to say this is dumb, and you really only need X. That may be true, but Windows developers get paid a lot more money than you, and don’t have time to waste trying to figure out the exact combination of things. I mean do I really need Hot Tamale 22.4 support? What about Lemonhead 5.5? By following this suggestion you WILL be installing things you don’t need, and it WILL take 23 hours instead of only 18 hours to install it all, but you probably won’t have to worry about your emulator working.

The Android SDK Manager. A great excuse to walk away from your computer for a few days.

NOTE: For those of you reading this and saying “Don’t use the Visual Studio Emulators for Android” you can stop right there. Windows developers need to use these emulators for a whole bunch of reasons, and the VS Emulators actually work better (and are lightning fast) on the typical Windows 10 development workstation. So if you’re a Google Emulator/HAXM person, chances are you also “lean native.”

The Wrap Up

That does it for this batch of Tips, Tricks, and Lessons Learned. If you were paying super-close attention in this post, you may have noticed a beautiful, well-written haiku. The first person to locate and verify my magical poetry will win a prize. Really. See you in Part 3. #XamarinRocks

, ,


  • David Lorenzin

    We left Xamarin after 5 years of heavy use, we developed lot of Forms apps, from small to big Enterprise.
    Apps are slow as hell (scrolling in primis), android apps randomly crashes, iOS debugging from VS is crazy, Forms still lacks Blend even after M$ acquisition one year ago.
    Back to ionic.

    • Scott J. Peterson

      We had these types of issues as well, but at this point in the Xamarin Forms timeline rarely have anything other than excellent performance and stability. Even a year ago I would have considered a move back to Ionic, but for 2017+ I’m super happy with Xamarin Forms. #Finally

      • David Lorenzin

        I don’t know where you see all that performance and stability, in medium/comes apps the app development becomes a bet.
        Not a solid SDK, at all. After trying other approaches, the native+extension s, or browser based ones win.

        • Scott J. Peterson

          It sounds like that is certainly your experience, and perhaps the experience of your peers, however it’s just not my experience or the experience of my peers. I have designed and developed literally hundreds of apps over the last 30 years, for organizations of all sizes, and of course the solutions have been of all sizes. Often these are medium to large-scale enterprise apps, with tons of legacy system integrations, and pretty demanding, picky users.

          It would be impossible for me to say “where” I see all the performance and stability enhancements of course, but in general I find that if my Xamarin Forms apps are not performing well it’s typically a poor design somewhere, but even where apps have business requirements “beyond” what I think a mobile app should do, I still find Xamarin Forms as stable, or more stable, as maintainable, or more maintainable, than any other platform for Microsoft developers to leverage.

          Again, I’m just sharing *my* experience, as a Microsoft Enterprise developer, having to create or architect a large number of solutions, for a large number of clients, every year, and having to do it quickly, while making sure what I hand over to internal teams is scalable, extensible, maintainable, and stable.

  • Pingback: Xamarin Forms for Windows Developers: Tips, Tricks and Lessons Learned, Part 1 - Wintellect DevCenterWintellect DevCenter()

  • Michael Rumpler

    Unfortunately the Visual Studio Emulator for Android is discontinued.
    Scott, is right, it did work better than all the others, but it stopped at Android 6.0. No newer versions will ever be supported.
    See https://blog.rthand.com/post/2017/05/02/good-bye-visual-studio-emulator-for-android-and-hello-problems.aspx

    PS: That doesn’t mean that you don’t have to install many of the Android SDK packages.