There are many ways to improve your productivity when developing a Xamarin.Forms application. Here, I have summarized a few things from my own experiences. The following are the phases of typical Xamarin.Forms application development:
- Design
- Develop business logic
- Build
- Test
- Deploy
The following are the most common tools and techniques that improve the life-cycle phases of developing an application in Xamarin.Forms that I will discuss in this blog:
- XAML
- Patterns
- Open-source libraries and frameworks
- Custom controls
- Rapid iteration using Hot Reload
- Build-time optimization
- Continuous integration
XAML
The appearance and performance of your application depends on how you organize and present UI controls in Xamarin.Forms. It is necessary to nest different layouts and controls to create your desired UI. Creating and editing different layouts and various controls in the code-behind will make it difficult to read and clumsy to work with. So, the better solution is to use XAML in all possible cases.
XAML code: easier to read and maintain
XAML code is easier to use and read when compared with its equivalent counterpart, C# code. Compare the following simple example XAML and C# code.
XAML | C# Code-Behind |
<StackLayout> <Label Text="{Binding Name}"/> </StackLayout> | StackLayout stackLayout = new StackLayout(); Label nameLabel = new Label(); nameLabel.SetBinding(Label.TextProperty, "Name"); stackLayout.Children.Add(nameLabel); |
XAMLC
A few months back, before recent Visual Studio and Xamarin updates, XAML files were embedded along with executables when deployed. Now, XAML files are compiled along with the C# files, known as XAMLC compilation. This feature is enabled by default when creating a Xamarin.Forms application (at the time of writing, I am using Visual Studio 2019 version 16.3.6). If you are not using the updated versions, still you can enable XAMLC by following the steps in this documentation.
How does enabling XAMLC boost your productivity?
- It performs compile-time checking of XAML, notifying the user of any errors.
- It helps reduce the file size of the final assembly by no longer including .xaml files.
Design-time data
Design-time data can be used to visualize a UI with mock data. It is very useful when you do not have the ViewModel before you start building the UI. Design-time data populates mock data in the XAML Previewer to visualize the XAML layout. Refer to this blog for more details.
Patterns
There are many patterns you can choose for your application. This Microsoft documentation outlines a few standard patterns. The most commonly used one is the Model-View-ViewModel (MVVM) pattern. This pattern helps to clearly separate the business and user interface logic of an application. Following the MVVM pattern will greatly help you maintain, test, and evolve your application by eliminating duplicate code and increasing the possibility of reusing code. It allows developers working on business logic and designers working on UX designs to work independently and simultaneously, improving the project’s productivity.
Open-source libraries and frameworks
It’s often accurate to apply the statement, “All the easy problems have already been solved,” to software development. Therefore, you just have to pick the right solution. Coming to Xamarin.Forms, there are many open-source libraries waiting to be used. You can skip most portions of the design phase of your application by using the following open-source projects:
- FFImageLoading: Library for loading images quickly and easily on Xamarin.iOS, Xamarin.Android, Xamarin.Forms, Xamarin.Mac/Xamarin.Tizen, and Windows.
- Essential UI Kit for Xamarin.Forms: Elegantly designed XAML templates for Xamarin.Forms applications. These templates are compatible with Android, iOS, and UWP platforms, and use the MVVM design pattern to provide trouble-free integration. You can get the complete design implementation for free from GitHub.
Though you can develop applications based on the MVVM pattern using the features available in the Xamarin.Forms framework, there are still certain items you have to do on your own, such as navigation services. Instead of writing your own implementation, there are many MVVM frameworks available you can use to save time. Some of the widely used frameworks are:
Custom controls
When you create a Xamarin application, most of the time, it is not possible to achieve your expected UI with the framework controls. So, you end up creating multiple custom controls and platform-specific renderers. Creating your own controls is a desirable choice, only if it doesn’t require too much work to create and maintain them.
Using third-party custom controls is a solution that reduces design and maintenance costs of your application. For this, Syncfusion—a market leader in Xamarin.Forms components—has 145+ Xamarin UI controls that can be used in your application for all scenarios, and provides simple and straightforward licensing.
Rapid iteration using Hot Reload
Until a few months ago, you had to build and deploy again and again to verify even the simplest changes in Xamarin.Forms applications. This was quite a time-consuming process, recompiling and redeploying the entire application for every little change. Hot Reload is a great feature recently introduced by the Xamarin team that addresses this. With the Hot Reload option enabled in Visual Studio, the changes you make in the XAML file are reflected live in your running app with the current application state. The following table lists the Visual Studio and Xamarin.Forms versions that support Hot Reload. Please refer to this blog for more details about the Hot Reload option.
IDE/Framework | Version Required |
Visual Studio 2019 | 16.3 or greater |
Visual Studio 2019 for Mac | 8.3 or greater |
Xamarin.Forms | 4.1 or greater |
Note: At .NET Conf 2019, the team announced Xamarin Hot Restart. The Hot Restart option will allow you to rapidly verify the changes you made in the code-behind and resource files. This is achieved by pushing the new assemblies directly into the existing app bundle, instead of redoing a complete build. This is going to be the most welcomed feature by Xamarin.Forms developers. Refer to this blog for more details about it, along with statistics showing its performance impact.
Build-time optimization
Building an application certainly takes a considerable amount of developers’ time, especially when it comes to building Xamarin.Forms for Android projects. There are a few properties that you can use to speed up the build process in Xamarin.Forms application. Refer to this blog for more details about the combination of settings that can be used to optimize build times.
Continuous integration
Though you can use Hot Reload and build optimization options, at the end of the day, someone must make the build that will be delivered to testers and end-users. We often come across a situation where the build works as expected on our end, but misbehaves on the customer’s end. Continuous integration (CI) is the best solution for these kinds of struggles. Not only for Xamarin.Forms, but for any domain or platform you work with, CI will improve your productivity without a doubt. To help you implement CI in your Xamarin.Forms projects, Visual Studio offers the App Center.
App Center can build the application for every commit you make in your repository, test the application with many virtual devices, and distribute the application to the various collaborators to use or test further. The most useful part of the App Center is it can report many analytical and diagnostic data points, like crash reports, user history over various modules of the application, and more. There are free and paid versions of App Center available. Refer to this documentation for more details.
Conclusion
The tips I listed in this blog are all from my own experiences, and I hope they will help you too. My colleagues and I invite you to see all our Xamarin.Forms controls in action by downloading a free trial. You can also explore our Android, iOS and UWP demo apps. You can learn about the controls’ advanced features in our documentation.
If you have any questions about these controls, please let us know in the comments below. You can also contact us through our support forum, Direct-Trac, or feedback portal. We are happy to assist you!
Comments (3)
Are you able to provide an example of design time data with the SfDataGrid as I’m unable to get it working, I can use design time data with a listview easily but not with your controls.
Hi Richard,
Your requirement to design-time sample for SfDataGrid is attached in the below Link, please refer the below sample for more details.
Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGridDemo-1685267748
Regards,
Balasubramani Sundaram
Well documented, gives good direction on where to start. thank you.
Comments are closed.