25,19 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 259
Veröffentlichungsjahr: 2021
Build apps with C# and XAML that run on Windows, macOS, iOS, Android, and WebAssembly
Matt Lacey
Marcel Alexander Wagner
BIRMINGHAM—MUMBAI
Copyright © 2021 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Associate Group Product Manager: Pavan Ramchandani
Publishing Product Manager: Rohit Rajkumar
Senior Editor: Sofi Rogers
Content Development Editor: Feza Shaikh
Technical Editor: Saurabh Kadave
Copy Editor: Safis Editing
Project Coordinator: Manthan Patel
Proofreader: Safis Editing
Indexer: Subalakshmi Govindhan
Production Designer: Vijay Kamble
First published: September 2021
Production reference: 1250821
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
978-1-80107-849-8
www.packt.com
Matt Lacey has been building desktop and mobile software since the '90s. He currently works as an independent development consultant and focuses on helping developers to create better software. Having worked in companies of all sizes and in a wide variety of industries, he brings this breadth of experience to present a viewpoint that considers technology, business, and design.
Matt is a Microsoft MVP in Windows Development, regularly speaks at user groups and conferences in multiple countries, and is a prolific contributor to a plethora of open source projects. He lives in the UK with his wife and two children.
Marcel Alexander Wagner is a full-stack software developer and open source contributor. He is a Microsoft MVP in Windows Development and a top contributor to the WinUI library and the XAML Controls Gallery, while also contributing to other projects and libraries, including the Windows Community Toolkit and Uno Platform.
Marcel graduated with a Bachelor of Science in computer science and has since been developing applications and services with a wide variety of technologies, including React, Java, C#, C++, UWP, and Uno Platform. He currently resides in Germany.
David Oliver is an open source framework developer with a .NET background. He is a recovering physicist and lapsed Australian. He is currently a senior developer on the Uno Platform core team.
Martin Zikmund is a freelance software developer and Microsoft Developer Technologies MVP. He specializes in building cross-platform mobile and cloud solutions on the Microsoft technology stack. His passion is contributing to open source, especially to Uno Platform. You can also encounter him on Stack Overflow, where he frequently helps other developers. To document his developer journey, Martin regularly writes articles on his blog and tweets about interesting things he comes across. In his spare time, he likes to play squash, game on Xbox, read, and search for geocaches.
Nick Randolph currently runs Built to Roam, which focuses on building rich mobile applications. He has been identified as a Microsoft MVP in recognition of his work and expertise with Microsoft application platforms.
Nick is an active contributor in the device application development space via his blog. He has been invited to present at a variety of events, including TechEd and Ignite Australia and NZ, DDD, NDC, and local user groups. He has also authored multiple books on Visual Studio and Windows development.
Shimmy Weitzhandler is a skilled full-stack developer and consultant working independently.
He has been coding for nearly two decades, and has used technologies ranging from ASP.NET, HTML, and JavaScript with VB.NET to WPF, Silverlight, WinUI, and, for the past few years, Uno Platform.
Among his projects are an emergency response system, a club card management and points accumulation portal, video conversion software, a school and college system, e-commerce websites, and QuickBooks components.
Shimmy is an active member and contributor on key tech hubs such as GitHub and Stack Overflow.
He is well known for his distinct programming style, his architectural thinking, and his dedication to creativity and transparency, utilizing the latest technologies.
This part of the book will provide you with all the information you need to know about Uno Platform and how to determine which of your projects it is appropriate for. It will then detail how to set up your development environment(s) for building apps with Uno Platform and walk you through creating your first app. It will then explore the basics of working with an app built with Uno Platform and show how you can use the tools and skills you're already familiar with. Additionally, it will show you how to do some of the most common tasks that developers need to do in most apps.
In this section, we include the following chapters:
Chapter 1, Introducing Uno PlatformChapter 2, Writing Your First Uno Platform AppUno Platform is a cross-platform, single-codebase solution for developing applications that run on various devices and operating systems. It does this while building on the rich heritage of Windows development APIs and tooling. This allows you to take the Windows app development skills you already have and use them to build apps for Android, iOS, macOS, WebAssembly, Linux, and others.
This book will be your guide to Uno Platform. It will show you how to use Uno Platform's functionality to build a variety of different applications that address real-world scenarios.
In this chapter, we'll cover the following topics:
Understanding what Uno Platform isUsing Uno PlatformSetting up your development environmentBy the end of this chapter, you'll understand why you'll want to use Uno Platform to develop apps, and the types of applications it's best suited to help you build. You'll also be able to set up your environment so that you're ready to start building apps when reading subsequent chapters in this book.
In this chapter, you will be guided through the process of setting up your development machine. To work through all the examples in the book, you will need a machine running any of the following:
Windows 10 (1809) or highermacOS 10.15 (Catalina) or higherIf you only have access to one, you'll still be able to follow along with most of the book. The book will primarily assume you are working with a Windows machine. We will only show examples that use Mac when absolutely necessary.
There is no source code for this chapter. However, the code for the other chapters can be found at the following URL: https://github.com/PacktPublishing/Creating-Cross-Platform-C-Sharp-Applications-with-Uno-Platform.
According to the website (https://platform.uno/), Uno Platform is "the first and only UI Platform for single-codebase applications for Windows, WebAssembly, iOS, macOS, Android and Linux."
That's a complex sentence so let's break down the key elements:
As a UI platform, it's a way of building applications with a User Interface (UI). This is in contrast to those platforms that are text-based and run from the command line (or equivalent), are embedded in hardware, or are interacted with in other ways, such as by voice.Using a single code base means you only need to write code once to have it run on multiple devices and operating systems. Specifically, this means the same code can be compiled for each platform the app will run on. This is in contrast with tools that convert or transpile code into a different programming language before being compiled for another platform. It's also the only code base that's singular, not the output. Some comparable tools create a unique package that runs inside a host application on each OS, or create everything in HTML and JavaScript, and run inside an embedded browser. Uno Platform does neither of these. Instead, it produces native application packages for each platform.Windows apps are based on the Universal Windows Platform (UWP) for Windows 10. Work is currently being done at Microsoft to make WinUI 3 the successor to UWP. Uno Platform has partnered with Microsoft to ensure that Uno Platform can easily transition from UWP once WinUI 3 is at a comparable operative level.Windows support also includes the Windows Presentation Foundation (WPF), powered by SkiaSharp, for apps that need to run on older versions of Windows (7.1 or 8.1).Applications that run in WebAssembly have all their code compiled to run inside a web browser. This means they can be accessed from any device with a compatible browser, without running code on the server.By supporting iOS, the apps that are created can run on iPhones and iPads.With support for macOS, the apps can run on a MacBook, iMac, or Mac Mini.Support for Android applies to phones and tablets running the Android operating system.Linux support applies to specific Linux PC equivalent distributions and is powered by SkiaSharp.Uno Platform does all of the preceding by reusing the tooling, APIs, and XAML that Microsoft created for building UWP apps.
Another way to answer the "what is Uno Platform?" question is that it's a way to write code once and have it run everywhere. The exact definition of "everywhere" is imprecise, as it doesn't include every embedded system or microcontroller capable of running code. Still, many developers and businesses have long had the desire to write code once and run it easily on multiple platforms. Uno Platform makes this possible.
One of the early criticisms of Microsoft's UWP was that it was only universal on Windows. With Uno Platform, developers can now make their UWP apps genuinely universal.
With the varied number of cross-platform tools available today, it's easy to forget how limited the options were back in 2013. At that time, there were no general-purpose tools for easily building native apps that ran on multiple operating systems.
It was at that time that nventive (https://nventive.com/), a Canadian software design and development company, faced a challenge. They had lots of knowledge and experience in building applications for Windows and Microsoft tools, but their customers were also asking them to create applications for Android and iOS devices. Rather than retrain staff or duplicate effort by building multiple versions of the same software for the different platforms, they invented a way to compile the code they wrote for Windows Phone (and later UWP) apps and transfer it to other platforms.
By 2018, it was obvious this approach had been successful for them. They then did the two following things:
They turned the tool they had created into an open source project, calling it Uno Platform.They added support for WebAssembly.As an open source project, this allowed other developers tackling the same problem to work together. Uno Platform has since seen thousands of contributions from over 200 external contributors, and involvement has been expanded to support more platforms and add additional functionality for the initially supported platforms.
As an open source project, it is free to use. Additionally, it is supported by a company with a business model that was made popular by Red Hat, and has been adopted widely. Usage is free and there is some free public support. However, professional support, training, and custom development are available only through payment.
Uno Platform works in different ways and uses multiple underlying technologies, depending on the platform you're building for. These are summarized in Figure 1.1:
If you're building an app for Windows 10, Uno Platform does nothing and lets all the UWP tooling compile and execute your app.If you're building an app for iOS, macOS, or Android, Uno Platform maps your UI to the native platform equivalents and uses native Xamarin libraries to call into the OS it is running on. It produces the appropriate native packages for each OS.If you're building a WebAssembly app, Uno Platform compiles your code against the mono.wasm runtime and maps the UI to HTML and CSS. This is then packaged into a .NET library that is launched with the Uno Platform web bootstrapper as static web content.To create Linux apps, Uno Platform converts your code to the .NET equivalent and uses Skia to create a version of the UI. It then outputs a .NET5 app that uses GTK3 to present the UI.Apps for Windows 7 and 8 are created by Uno Platform by wrapping the compiled code in a simple WPF (NETCore 3.1) app that uses SkiaSharp to render the UI.Refer to the following diagram:
Figure 1.1 – The high-level architecture of Uno Platform
Whichever operating system or platform you're building for, Uno Platform uses the native controls for the platform. This enables your apps to achieve the experience and performance of a fully native app. The exception to this is where it uses SkiaSharp. By using SkiaSharp, Uno Platform draws all UI content on a canvas rather than using platform-native controls. Uno Platform does not add an extra layer of abstraction to the running app (as you might find with cross-platform solutions that use a container, such as an embedded WebView within a shell app).
Uno Platform enables you to do a lot with a single code base. But can it do everything?
The principle of writing code once and running that code everywhere is both powerful and appealing. However, it's necessary to be aware of the following two key points:
Not all applications should be created for all platforms.It's not an excuse for not knowing the platforms the apps will run on.Additionally, not everything warrants an app. Suppose you just want to share some information that won't be frequently updated. In such a scenario, a website with static web pages would likely be more appropriate.
The lesson just because you can do something doesn't mean you should applies to applications too. When you see how easy it is to create applications that run on multiple platforms, you may be tempted to deploy your applications everywhere you can. Before you do this, there are some important questions you need to ask:
Is the app wanted or needed on all the platforms? Do people want and need to use it on all the platforms you make it available? If not, you may be wasting effort by putting it there.Does the application make sense on all the platforms? Suppose the application has key functionality that involves capturing images while outside. Does it make sense to make it available on a PC or Mac? In contrast, if the application requires the entry of lots of information, is this something people will want to do on the small screen of a mobile phone? Your decision about where to make an application available should be determined by its functionality and the people who will use it. Don't let your decision be based solely on what's possible.Can you support it on all platforms? Does the value you gain by making an application available on a platform justify the time and effort in releasing, maintaining, and supporting the application on that platform? If you only have a small number of people use the app on a particular type of device, but they generate many support requests, it's OK to reevaluate your support for such devices.No technology will render a perfect solution for all scenarios, but hopefully, you can already see the opportunity that Uno Platform provides. Let's now look a bit closer at why and when you might want to use it.
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 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.
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
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.
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
