Rules to Better Xamarin (mobile)
Xamarin has evolved beyond simply being an abstraction of the platform native APIs for iOS, Android, and UWP. Where previously you could write shared business logic in C# but would have had to have written your UI in either Xamarin.Android or Xamarin.iOS, now you can write cross-platform UI code in Xamarin.Forms. Theoretically, it is possible to write your code only once and share 100% of the code you write across all target platforms, and at least 96% - see Adam Cogan's blog post.
It is possible to implement beautiful and complex UI designs in Xamarin.Forms, especially given the rich ecosystem of plugins and templates available (see rule: Do you know where to find the best Xamarin resources?). Using drawing APIs like SkiaSharp or the new shapes and path APIs built into Xamarin.Forms (Note: this is an experimental feature) there is no UI that you can't implement cross-platform. Even so, 100% of the platform native APIs are exposed in Xamarin, so if you wanted to you could still implement any UI natively on each platform and call it using your shared code with a custom renderer.
Picking the right development environment is important, and which platforms you want to target will influence that decision.
Note: More platforms are coming in 2021 when Xamarin.Forms evolves into MAUI, see: Introducing .NET Multi-platform App UI.
If you want to develop for Android, wearOS, or Tizen, you can use Visual Studio on either Windows or macOS. If you want to target UWP, you must use Windows. If you want to develop for iOS, tvOS, macOS, or watchOS, you can now develop using Windows or Mac (using Hot Restart on Windows) but must use a Mac to publish your app to the App Store. If you want to target all these platforms you will need access to both Windows and Mac.
Tip: If you use a Mac you can run Windows through virtualization, using VMware Fusion, Parallels or VirtualBox. If you use Windows, there are cloud-based Mac services you can use for your Apple OS builds.
Xamarin follows the MVVM design pattern (see: Do you use the MVVM pattern in your Silverlight and WPF Projects?. MVVM was originally developed for WPF but is the dominant paradigm in Xamarin development (at least until 2021 when Xamarin.Forms evolves into MAUI, which will make the MVU pattern a first-class citizen. See: Introducing .NET Multi-platform App UI.)
MVVM allows for loose coupling between data, business logic, and UI. In Xamarin, UI is usually defined in XAML (although you can declaratively define your UI in C# code too). Your UI is called a 'view' - a view can be a page or a UI element, although UI elements that are not complete pages are more often referred to as controls.
Xamarin supports MVVM out of the box, but there are several MVVM frameworks available that enhance this functionality. For example, some MVVM frameworks support "convention over configuration", allowing you to just code your View and ViewModel and let the framework hook them up for you. Some include:
- Prism: Prism is an MVVM framework that was developed initially for WPF but has since been ported to Xamarin Forms. It is stable and mature.
- FreshMVVM: FreshMVVM is a framework that was built from the ground up specifically for Xamarin Forms. It is open-source and maintained by a Microsoft and Xamarin MVP.
- MVVMLight: This is another framework built especially for Xamarin, although it is not currently under active maintenance.
The XAML previewer in Visual Studio is a useful tool for designing your Xamarin UI. One limitation is that often your controls are bound to properties in your ViewModel (see rule: Do you use the MVVM pattern?), meaning that you can't see what your UI will look like with the data loaded, without building and running your app.
A simple solution to this problem is to use design-time data. By importing the relevant namespaces into your XAML file, you can specify placeholder data that the previewer interprets to show how your UI will render with data loaded.
These are the namespaces to import, and the declaration to use them:
xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
If your Xamarin and Visual Studio versions are up to date these namespaces will automatically be included in any new XAML file.
Developing mobile apps presents unique challenges compared to web or desktop development. One of the problems is that when using MVVM or using dynamic data on a page, you need to run your app to populate the data and see what your UI will actually look like.
This problem is partially solved by Design Time Data (see rule: Do you use design time data?), but this still doesn't show you how things change as you interact with them.
To get around this problem use Hot Reload. This lets you make changes to your XAML while debugging your app - as soon as you save your UI will update, without having to stop and rebuild your app.
Tip: This works on the iOS simulator, the Android emulator, and physical iOS and Android devices.
Branding is important in any product, and especially a mobile app. Xamarin offers several ways to define and ensure consistent styling throughout your app:
- Resource Dictionaries
Resource Dictionaries and CSS provide similar capabilities, but Resource Dictionaries are the best way to style your Xamarin applications. CSS in Xamarin is not full, web-standards compliant CSS, but rather an alternative way to write Xamarin styles than Resource Dictionaries. CSS in Xamarin does not currently support the full range of properties available, but it may be more comfortable for those familiar with web development.
Visual offers a much more granular level control over the look and feel of your application. However, this increase in granularity comes with a proportionally increased level of complexity. Rather than providing a style, you need to define a custom renderer for every control you want to define the look of.
Visual is suitable for large teams with a design heavy focus, where branding is of paramount importance. The advantage of Visual is that you can specify it at individual control level, at page level, or at whole of app-level to ensure your entire app is consistent.
mFractor is the best tool for managing assets in your Xamarin project: https://www.mfractor.com/products/mfractor-professional
This Visual Studio extension (now for Windows and Mac) is valuable for Xamarin work, it provides time-saving short cuts for several things, including (but not limited to):
- Model and viewmodel creation
- Intellisense between views and viewmodels
- Importing of assets (images, fonts, etc)
Importing images is the 'killer feature' - with one click you can import images into all the required folders. Without it, you need to create individual image assets for each device resolution and manually copy them to the corresponding folder for iOS and Android, and then update the manifest XML files. Using mFractor saves 30-60 minutes for every image imported into a project.
Whereas iOS enforces AOT (ahead of time) compilation, Android supports (and uses by default) JIT (just in time) compilation, but AOT can be enabled on Android to improve performance.
Xamarin also provides several other options for optimizing Android builds and improving performance. Additionally, you can use Android's new bundle format (rather than apk) so that smaller builds can be targeted to individual hardware specifications.
For more information, see: https://devblogs.microsoft.com/xamarin/optimize-xamarin-android-builds/
Any changes you make to your app risk breaking existing functionality. Having a suite of automated tests that you can run prior to any release reduces the risk of releasing a product with new features that don't work, or that breaks existing features. It also means that you can run these tests as part of your CI/CD pipeline, or with a dedicated testing infrastructure such as Xamarin Test Cloud in Visual Studio App Center, which runs your tests on real devices.
Xamarin comes with a built-in UI testing framework, Xamarin.UITest. This allows you to write UI tests that ensure your app's UI behaves as expected. UI tests in Xamarin.Forms follow the Page Object Pattern (POP), see: https://channel9.msdn.com/Events/Xamarin/Xamarin-Developer-Summit-2019/Page-Object-Pattern-and-UITest-Best-Practices
The following is a list of resources for learning, staying up to date with, and using Xamarin.
Awesome Xamarin Forms is a curated list on GitHub of a bunch of resources for Xamarin. This includes controls, effects, behaviors, tools, and more. It's a good 'one-stop-shop' for everything Xamarin.
Weekly Xamarin is a weekly newsletter sent every Friday or Saturday. It's a community-curated list of all the latest Xamarin news, blog posts, and podcasts.
The best video courses for learning Xamarin are Asfend Yar Hamid's real-world app series on Udemy.
The best book for learning Xamarin is [Xamarin.Forms Projects by Daniel Hindrikes and Johan Karlsson](https://www.packtpub.com/product/xamarin-forms-projects-second-edition/9781839210051).
SSW hosts a Xamarin Hack Day every year.
Xamarin has a thriving and passionate community and ecosystem. Many Xamarin developers have made awesome controls available either as open-source libraries, NuGet packages, or both. A lot of these can be found at the Awesome Xamarin GitHub page linked above. Additionally, a number of commercial options are available.
Xamarin Community Toolkit: This is a community-driven set of controls, effects, behaviors, and extensions that make add a lot of functionality you are likely to reuse in your projects.
SyncFusion: SyncFusion provides a set of beautiful controls and templates for a range of use cases and scenarios. There is also a community license, which allows you to use it free for anything earning less than $1m a year.
Telerik: Progress has made their Telerik UI available for Xamarin.Forms. Telerik provides a range of controls focused on enterprise and business applications.
The best Xamarin stream is Kym Philpott's weekly stream on Twitch.
There is a strong community of Xamarin streamers regularly producing awesome content. A curated list can be found at the URL List of Xamarin streamers.