Cross-platform UIs ‘go live’ with .NET MAUI
The cross-platform UI “runs” in .NET MAUI Much of the current vision of .NET builds on the work of the Mono and Xamarin teams to provide a cross-platform version of the .NET runtime. Updating .NET Core is the first part of the project: creating a way to run your code on Windows, macOS, Linux, Android, iOS, and more. Now it’s time for part of the user experience to get a similar update. Win UI 3 and Windows App SDK provide user interface tools for Windows apps and MAUI, the cross-platform app user interface, provides you with tools to create cross-platform apps for desktop and mobile devices. Originally intended as part of the .NET 6 release wave, MAUI turned out to be a more complex project than originally intended. However, it is almost ready, a release candidate is already available, and general availability is expected during Build 2022. The arrival of a release candidate is an important milestone because this is the point in the software development life cycle that Microsoft offers. offers the call “run license live”. Time to “come live” for MAUI Up until this point, the preview versions were intended for personal use to learn new tools and techniques. You can start porting the code to new frameworks as soon as the examples arrive, but they are not complete and will change from release to release. This ends with the dismissal of the candidates; the code is frozen and all further changes will be fixed. The “go live” feature is also useful here as it has support. Microsoft can see interactions between their code and yours, which helps create patches that are tested against real-world scenarios. [Also on InfoWorld: Understanding .NET MAUI and the Future of Xamarin] If you’ve used Xamarin Forms, MAUI will be very familiar. It’s a way to host operating system-specific controls and UI elements in a single codebase. Instead of different projects and solutions for desktop, mobile, and each operating system, all of your code is in one project, ensuring that there’s no difference between the app running on each target—except that each build will have a native look and feel with native management. While Xamarin Forms is mobile only, MAUI adds support for desktop environments. There is even an option to host Blazor Razor code on MAUI with integrated web view controls to natively run Razor components on your target device. Platform integration You can best think of MAUI as a way to integrate various platform-specific .NET APIs so that C# and XAML code can be written once and run anywhere, with the ability to provide platform-specific code to avoid the lowest common denominator approach. MAUI sits on top of native code as well as regular base class libraries. Your code calls the MAUI API, which calls the required platform API. If you want to have native specific functions, you can call platform APIs directly if they are not covered by MAUI. This approach gives you a basic set of common controls like those used in Xamarin Forms, with a layout module that allows your UI code to scale between different form devices and screen sizes. Knowing the capabilities of your target devices is important when creating user interface designs that can support the transition between landscape PC and Mac environments and portrait mobile screens. A big part of MAUI is the familiar XAML design, with a page description and code-behind to manage interactions with the rest of your app, as well as a canvas to display and interact with custom graphics. It also includes a set of APIs that provide access to device features. It gives you a common way to work with GPS and other sensors like battery health and device networks. Construction of MAUI projects MAUI projects are organized in a similar way to the approach introduced in Xamarin Forms. A project contains all your code, with any platform-specific code organized by goals. You work with common code in the main part of the project and drop specialized code as needed. For example, you can have a set of platform code for GPS support, with separate Windows and Android code versions used at build time, built based on platform goals. Your Windows app will use the Windows GPS API and the Android version will call the Android version. Both versions of the platform share the same approach, wrapping the API in a common call and return structure. MAUI has direct support from Microsoft for Android 5.0 and above, iOS 10 and above, Windows 10 1809 and above, and macOS 10.13. The macOS code will be similar to iOS (and iPadOS) as it uses the recently released macOS Catalyst tools that allow you to bring iPad apps to the desktop, automatically add features to the desktop, and add support for many of Apple’s core APIs and services. Additional support from Samsung allows MAUI to run on Tizen, and the .NET community is developing support for Linux. It pays to use MAUI in the latest Visual Studio Preview, which adds support for .NET and XAML hot reload. It is a basic tool for debugging and development, so you can make changes to both the user interface and the application code while the application is running in a test environment and reload the changes. This technique saves a lot of time, and it’s good to see that it was built into .NET 6 at the last minute (after many developers complained about it being removed from examples later in the development cycle). Start coding on MAUI today Creating your first app is easy. Start by configuring Visual Studio to support mobile development with .NET. It adds support for both Xamarin and MAUI, so you can port existing Xamarin apps to MAUI or continue to support older versions. Visual Studio now offers the ability to build .NET MAUI applications using standard .NET techniques or the Blazor framework. Once you create a project, MAUI automatically sets up dependencies for the target OS, installs and configures the Android emulator and SDK. Apple licensing means that to develop iOS and macOS apps you need a Mac running on the same network as your development computer with the appropriate .NET SDK installed along with the current version of Xcode. Microsoft’s move to a common cross-platform .NET has made it easier to provide tools like MAUI. Although Xamarin used to pay off if you’re building mobile apps, the learning curve remains small, especially if you’re using it as a framework for building Windows apps with WinUI. It makes sense to start thinking of MAUI as the foundation for all your future .NET GUI development, no matter what your goals are. Yes, you can now only build an Android .NET app, but MAUI makes it easy to take that code and use it on any other supported platform with minimal modifications. That’s a big win for very little work.