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.
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.
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.
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.
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
Need Xamarin Help?
Xamarin Consulting Xamarin Training