Book Image

Creating Cross-Platform C# Applications with Uno Platform

By : Matt Lacey, Marcel Alexander Wagner
Book Image

Creating Cross-Platform C# Applications with Uno Platform

By: Matt Lacey, Marcel Alexander Wagner

Overview of this book

Developers are increasingly being asked to build native applications that run on multiple operating systems and in the browser. In the past, this would have meant learning new technologies and making multiple copies of an application. But the Uno Platform allows you to use tools, languages, and APIs you already know from building Windows apps to develop apps that can also run on other platforms. This book will help you to create customer-facing as well as line-of-business apps that can be used on the device, browser, or operating system of your choice. This practical guide enables developers to put their C# and XAML knowledge to work by writing cross-platform apps using the Uno Platform. Packed with tips and practical examples, this book will help you to build applications for common scenarios. You'll begin by learning about the Uno Platform through step-by-step explanations of essential concepts, before moving on to creating cross-platform apps for different lines of business. Throughout this book, you'll work with examples that will teach you how to combine your existing knowledge to manage common development environments and implement frequently needed functionality. By the end of this Uno development book, you will have learned how to write your own cross-platform apps with the Uno Platform and use additional tools and libraries to speed up your app development process.
Table of Contents (13 chapters)
1
Section 1: Getting to Know Uno Platform
4
Section 2: Writing and Developing Uno Platform Apps
9
Section 3: Test, Deploy, and Contribute

Using Uno Platform

Now you know what Uno Platform is, we'll look at what you need to consider when choosing whether to use it. There are four factors to consider:

  • What you already know.
  • What platforms do you wish to target?
  • The functionality required in the app.
  • How it compares to alternatives.

Let's explore each of these factors in relation to Uno Platform.

Uno Platform allows you to use what you already know

Uno Platform was initially created for developers using C# and XAML within Visual Studio. If this is familiar to you, this will make it easy to start using Uno Platform, as you'll be working with the software you already know.

If you're already familiar with UWP development, the differences will be minimal. If you're familiar with WPF development, there are minor differences in XAML syntax and available functionality. As we go through the book, you'll learn everything you need to build with Uno Platform. As long as you don't expect everything to work as it does in WPF, you'll be fine. Also, as the WinUI and Uno Platform teams are working to remove the minor differences that exist, you may never notice a difference.

If you don't know C# or XAML, Uno Platform may still be suitable for you, but as this book assumes familiarity with these languages, you may find it helpful to first read C# 9 and .NET 5 – Modern Cross-Platform Development – Fifth Edition, Mark J. Price, Packt Publishing, and Learn WinUI 3.0, Alvin Ashcraft, Packt Publishing.

Uno Platform supports many platforms

One of the great things about Uno Platform is the number of platforms it allows you to build for. Uno Platform has support for the most common platforms, but if you need to build applications that run on a niche platform or specialist device, then it may not be suitable for you. Additionally, if you need to support an old version of a platform or operating system, you may have to find workarounds or alternative solutions. The following table shows the versions of the supported platforms you can build for with Uno Platform:

Figure 1.2 – The lowest supported platform versions supported by Uno Platform

Figure 1.2 – The lowest supported platform versions supported by Uno Platform

Support for multiple platforms can also be advantageous, even when you want very different application behaviors or functionalities across different platforms. It's possible to support multiple platforms by creating multiple solutions, rather than by combining everything into a single solution.

Uno Platform boasts up to 99% reuse of code and UI. This is great when you need the same thing on all devices. However, if you require different behavior or a UI that's highly customized for different platforms (something that we'll look into in future chapters), it can be easier to build the different applications in different solutions, as opposed to putting lots of conditional logic in the code. There is no hard and fast rule for how much conditional code is too much, and it varies based on project and personal preference. Just remember it remains an option if you ever find your code is becoming full of conditional comments that make it hard to manage.

Accordingly, it's also possible to use Uno Platform to build for a single platform. You may not want to create an app that runs everywhere. You may only be interested in a single platform. If that's the case, you can use Uno Platform for that too. It also makes it easy to add additional platforms in the future if your needs change.

Can Uno Platform do everything that your app requires?

Core to Uno Platform's ability to reuse the UWP APIs to build for other platforms is that it has code to map the UWP API to its equivalent on the other platforms. Due to time, practicality, and priority limitations, not all APIs are available on all platforms. By way of general guidance, the most common APIs are available on the broadest number of platforms. Suppose you need to use more specialist functionality or are targeting something other than Android, iOS, Mac, or WebAssembly? In that case, it's advisable to check that the features you need are available.

Tip

We recommended confirming that the functionality you need for your app is available before you start writing code. This will allow you to avoid any nasty surprises late in the development process.

Due to the permanence of printed books and the frequency with which new functionality is added and more APIs are supported, it's not appropriate to list what is supported here. Instead, you can see a high-level list of supported features at the following URL: https://platform.uno/docs/articles/supported-features.html. There's also a list of supported UI elements at the following URL: https://platform.uno/docs/articles/implemented-views.html. Of course, the definitive way to confirm what is and isn't available is to check the source code at the following URL: https://github.com/unoplatform/uno.

If you try and use an API that is not supported, you'll see a hint inside Visual Studio, as is shown in Figure 1.3. If you try and use this at runtime, you'll either get nothing (a NOOP) or a NotSupported exception:

Figure 1.3 – An example of an unsupported API being indicated in Visual Studio

Figure 1.3 – An example of an unsupported API being indicated in Visual Studio

If necessary, you can check for supported features at runtime by using the Windows.Foundation.Metadata.ApiInformation class.

As an open source project, there is also the option to add any currently unsupported features yourself. Contributing such an addition back into the project is always greatly appreciated and new contributors are always welcomed by the team.

How does Uno Platform compare to the alternatives?

As mentioned earlier, many tools are available for developing applications that run on more than one platform. It is not our intention to discuss all the options available, as they can be evaluated and compared with the previous three points. However, as this book is intended for developers already familiar with C#, XAML, and Microsoft technologies, it is appropriate to mention Xamarin.Forms.

Xamarin.Forms was created at around the same time as Uno Platform and has several similarities. The two key ones are using C# and XAML to create apps that run on multiple operating systems. Both do this by providing an abstraction over the Xamarin.iOS and Xamarin.Android libraries that contain the C# bindings to the underlying operating systems.

The two biggest differences between Uno Platform and Xamarin.Forms are as follows:

  • Uno Platform supports building for a greater number of platforms.
  • Uno Platform reuses the UWP APIs and XAML syntax, rather than building a custom one.

The second point is important for developers already familiar with UWP development. The names of many Xamarin.Forms elements and properties are similar-sounding, so remembering the variations can be challenging.

Version 5 of Xamarin.Forms was released toward the end of 2020 and is intended to be the last version of Xamarin.Forms. It will be replaced with .NET Multi-platform App UI (MAUI) as part of .NET 6. .NET MAUI will support building apps for iOS, Android, Windows, and Mac from a single code base. However, it will not include the ability to also build for WebAssembly. Microsoft already has Blazor for building for WebAssembly, and so is not looking to add this capability to .NET MAUI.

.NET 6 will bring with it many new capabilities. Some of these capabilities are being added specifically for .NET MAUI. Once part of .NET 6, these capabilities will not be limited only to .NET MAUI. They will be available to Uno Platform apps too. The most obvious of these new capabilities is in having a single project that can produce different outputs for different platforms. This will enable a significant simplification of the required solution structure.

Important note

As we write this book, Microsoft is preparing to release WinUI 3 as the next-generation Windows development platform. This will build upon UWP and is part of the Project Reunion effort to make all Windows functionality and APIs available to developers, regardless of the UI framework or application packaging technology they use.

As WinUI 3 is the successor of UWP development, the Uno Platform team has publicly stated that plans and preparations are underway for Uno Platform to transition to using WinUI 3 as the base upon which it builds. This is being done in partnership with Microsoft, allowing the Uno Platform team to take the WinUI code and modify it to work elsewhere. You can be confident that anything you make now will have a path to transition to and take advantage of the benefits and functionality that WinUI will bring.

Another similar cross-platform solution that uses XAML to define the UI of an app is Avalonia (https://avaloniaui.net/). This, however, is different in that it focuses only on applications for desktop environments.

As you now have a solid understanding of what Uno Platform is and why you'll want to use it, you'll need to set up your machine so you can write code and create apps.