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

Setting up your development environment

Now that you are familiar with Uno Platform, you're undoubtedly eager to begin writing code. We'll start that in the next chapter, but you'll need to set up your development environment before we can begin.

Visual Studio is the most popular Integrated Development Environment (IDE) for developing Uno Platform apps. A large part of this is because it has the broadest set of capabilities and the best support for building UWP apps.

Developing with Visual Studio

To build apps with Uno Platform using Visual Studio, you will need to do the following three things:

  • Ensure you have Visual Studio 2019 version 16.3 or higher, although using the latest version is recommended.
  • Install the necessary workloads.
  • Install the project and item templates.

Installing the required workloads

The many tools, libraries, templates, SDK, and other utilities that can be installed as part of Visual Studio are collectively called components. With over 100 components available, related components are grouped into workloads to make it easier to choose what you need. You select workloads in the Visual Studio Installer, and these are shown in Figure 1.4:

Figure 1.4 – The Visual Studio Installer showing various workload options

Figure 1.4 – The Visual Studio Installer showing various workload options

To build apps with Uno Platform, you'll need the following workloads installed:

  • Universal Windows Platform Development
  • Mobile development with .NET
  • ASP.NET and web development
  • .NET Core cross-platform development

Installing the required templates from the marketplace

To make it easier to build your Uno Platform applications, multiple project and item templates are available. These are installed as part of the Uno Platform Solution Templates extension. You can install this from within Visual Studio, or directly from the marketplace.

Installing templates from within Visual Studio

To install the extension containing the templates, perform the following actions within Visual Studio:

  1. Go to Extensions>Manage Extensions.
  2. Search for Uno. It should be the first result.
  3. Click the Download button.
  4. Click Close, let the extension installer complete, and then restart Visual Studio:
Figure 1.5 – Uno Platform Solution Templates shown in the Manage Extensions dialog

Figure 1.5 – Uno Platform Solution Templates shown in the Manage Extensions dialog

Installing templates from the marketplace

Follow these steps to install the extension from the marketplace:

  1. Go to https://marketplace.visualstudio.com and search for Uno. It should be the first result returned.

    Alternatively, go directly to the following URL: https://marketplace.visualstudio.com/items?itemName=nventivecorp.uno-platform-addin.

  2. Click on the Download button.
  3. Double-click on the downloaded .vsix file to start the install wizard.
  4. Follow the steps in the wizard.

With the workloads and templates installed, you're now ready to start building apps. However, if you want to develop for iOS or Mac, you'll also need a Mac device set up so that you can connect to it from Visual Studio on Windows.

Using other editors and IDEs

It's not compulsory to use Visual Studio 2019 on a Windows PC, and the Uno Platform team has worked hard to make building Uno Platform apps as flexible as possible. Therefore, you can use it within your existing working patterns and preferences.

Installing the required templates with the command line

In addition to working with the templates inside Visual Studio, it's also possible to install them for use from the command line. To install them this way, run the following at the command line or terminal:

dotnet new -i Uno.ProjectTemplates.Dotnet

After this command has finished, it will list all the available templates. You should see multiple entries with a short name, beginning with uno.

Building Uno Platform apps with Visual Studio for Mac

To build Uno Platform apps using Visual Studio for Mac, you will require the following:

  • Visual Studio for Mac version 8.8 or higher (using the latest version is recommended).
  • Xcode 12.0 or higher (using the latest version is recommended).
  • An Apple ID.
  • .NET Core 3.1 and 5.0 SDKs.
  • GTK+3 (for running the Skia/GTK projects).
  • The templates installed (see previous section).
  • Enable the templates to be visible in Visual Studio for Mac by opening the Preferences menu option and then selecting Other>Preview Features and checking Show all .NET Core templates in the New Project Dialog.

Links to all these are available at the following URL: https://platform.uno/docs/articles/get-started-vsmac.html.

Building Uno Platform apps with Visual Studio Code

You can use Visual Studio Code to build WebAssembly apps on Windows, Linux, or Mac. Using it to build apps for other platforms is not yet supported.

To build Uno Platform apps using Visual Studio Code, you will need the following:

  • Visual Studio Code (using the latest version is recommended)
  • Mono
  • .NET Core 3.1 and 5.0 SDKs
  • The templates installed (see previous section)
  • C# extension for Visual Studio Code
  • JavaScript Debugger (Nightly) extension for Visual Studio Code

Links to all these are available at the following URL: https://platform.uno/docs/articles/get-started-vscode.html.

Building Uno Platform apps with JetBrains Rider

It is possible to use JetBrains Rider on Windows, Mac, and Linux, but not all platforms can be built for with all versions.

To build Uno Platform apps with JetBrains Rider, you will need the following:

  • Rider version 2020.2 or higher, although using the latest version is recommended
  • Rider Xamarin Android Support Plugin
  • .NET Core 3.1 and 5.0 SDKs
  • The templates installed (see previous section)

There are some additional points to be aware of when using JetBrains Rider, as follows:

  • WebAssembly apps cannot yet be debugged from within the IDE. As a workaround, it's possible to use the Chromium in-browser debugger instead.
  • If building the Skia/GTK projects on a Mac, you'll also need to install GTK+3.
  • If you wish to build iOS or Mac apps using a Windows PC, you will need an attached Mac (as you would if using Visual Studio).

Links to all these and more details are available at the following URL: https://platform.uno/docs/articles/get-started-rider.html.

Important note

It is also possible to use Blend for Visual Studio (on Windows) to work with code as you can for regular UWP apps. However, Blend does not support all the project types that an Uno Platform solution contains. You may find it beneficial to have a separate version of the solution that doesn't include those projects, and access that version in Blend.

Checking your setup

Uno Platform has a dotnet global tool to check if your machine is set up correctly and walk you through addressing any issues it finds. It's called uno-check and it's very simple to use, as follows:

  1. Open a developer Command Prompt, Terminal, or PowerShell window.
  2. Install the tool by entering the following:
    dotnet tool install --global Uno.Check
  3. Run the tool by entering the following:
    uno-check
  4. Follow any prompts it gives you and enjoy looking at the following message: Congratulations, everything looks great!

Debugging your setup

Whichever IDE or code editor you use, there will be many parts, and the use of multiple tools, SDKs, and even machines can make it hard to know where to begin when things aren't working. The following are general tips to help work out what isn't working. Some of these may seem obvious, but I'd rather look a fool for reminding you to check something obvious than have you waste time on an unchecked assumption:

  • Try restarting your machine. Yes, I know, it would be funny if it didn't work so often.
  • Read and then re-read any error messages carefully. They can sometimes be helpful.
  • Check you have installed everything correctly.
  • Has anything changed? Even if you didn't do it directly, something might have been changed automatically or without your knowledge (including, but not limited to, OS updates, security patches, IDE updates, other apps being installed or uninstalled, and network security permission changes).
  • If one thing has been updated, have all dependencies and referenced components been updated too? It's common that when things are connected, share references, or communicate, they must be updated together.
  • Have any keys or licenses expired?
  • If there is a problem with a previously created app, can you create a new app and compile and run that?
  • Can you create a new app and confirm that it compiles and runs on each platform?
  • If on Windows, can you create a new blank UWP app and then compile and debug it?

Trying equivalent actions or creating equivalent apps with other tools can often produce different error messages. In addition, you may also find paths to solutions that fix problems in your Uno Platform project setup:

  • If using a WebAssembly app, can you create a new, blank ASP.NET web app or Blazor project and compile and debug that?
  • If a WebAssembly app doesn't work in one browser, are error messages shown in the browser log or debug window? Does it work in another browser?
  • For Android, iOS, or macOS issues, can you create, compile, and debug Xamarin.Forms apps?
  • If there is an Android-specific issue, can you create and debug an app with Android Studio?
  • If using a Mac, can you create and debug a blank app with Xcode?

Additional tips for resolving common setup and configuration issues can be found at the following two URLs:

If the issue comes from connecting to a Mac from a PC, the Xamarin documentation may be helpful. It is available at the following URL: https://docs.microsoft.com/en-us/xamarin/ios/get-started/installation/windows/connecting-to-mac/. This can also help identify and address the issue in Uno Platform projects as well.

Details on where to go for answers to specific Uno Platform-related questions can be found in Chapter 8, Deploying Your Apps and Going Further.