Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
36
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Introducing

.NET MAUI

Build and Deploy Cross-platform

Applications Using C# and .NET

Multi-platform App UI

Shaun Lawrence

Introducing

.NET MAUI

Build and Deploy

Cross-platform Applications

Using C# and .NET

Multi-platform App UI

Shaun Lawrence

Introducing .NET MAUI: Build and Deploy Cross-platform Applications Using C# and .NET Multi-platform App UI

Shaun Lawrence

St Ives, UK

ISBN-13 (pbk): 978-1-4842-9233-4\

 

ISBN-13 (electronic): 978-1-4842-9234-1

https://doi.org/10.1007/978-1-4842-9234-1

 

Copyright © 2023 by Shaun Lawrence

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Managing Director, Apress Media LLC: Welmoed Spahr

Acquisitions Editor: Joan Murray

Development Editor: Laura Berendson

Editorial Assistant: Gryffin Winkler

Copy Editor: Mary Behr

Cover image designed by mrsiraphol through Freepik (www.freepik.com)

Distributed to the book trade worldwide by Springer Science+Business Media LLC, 1 New York Plaza, Suite 4600, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.

Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales.

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub using this link: https://github.com/Apress/Introducing-MAUI.

Printed on acid-free paper

Table of Contents

About the Author\ xv

About the Technical Reviewer\ xvii

Acknowledgments\ xix

Introduction\ xxi

Part I: Getting to Know .NET MAUI\ 1

Chapter 1: Introduction to .NET MAUI\ 3

What is .NET MAUI?\ 3 Digging a Bit Deeper 5 Where Did It Come From? 6 How It Differs From the Competition\ 7 Why Use .NET MAUI? 8 Supported Platforms 8 Code Sharing\ 9 Developer Freedom\ 10 Community\ 10 Fast Development Cycle\ 10 Performance 11 Strong Commercial Offerings\ 12

iii

Table of Contents

Limitations of .NET MAUI\ 13 No Web Assembly (WASM) Support\ 13 No Camera API 13 Apps Won’t Look Identical on Each Platform\ 14 Lack of Media Playback Out of the Box\ 14

The Glass Is Half Full, Though\ 14 How to Build .NET MAUI Applications 15 Visual Studio 15 Summary\ 17

Chapter 2: Building Our First application\ 19

Setting Up Your Environment\ 19 macOS\ 19 Windows\ 27 Visual Studio to macOS\ 28 Troubleshooting Installation Issues 32

.NET MAUI Workload Is Missing 32 Creating Your First Application\ 33 Creating in Visual Studio\ 33 Creating in the Command Line\ 37 Building and Running Your First Application\ 38 Getting to Know Your Application\ 41 WidgetBoard\ 41 Summary\ 42 Source Code\ 43

iv

Table of Contents

Chapter 3: The Fundamentals of .NET MAUI\ 45

Project Structure 45 /Platforms/ Folder 48 /Resources/ Folder\ 51 Where To Begin?\ 56 Generic Host Builder\ 56 What Is Dependency Injection?\ 57 Registering Dependencies 60 Application Lifecycle\ 62 Application States\ 62 Lifecycle Events 64 Handling Lifecycle Events 65 Cross-Platform Mappings to Platform Lifecycle Events\ 66 Platform-Specific Lifecycle Events 67 Summary\ 72

Chapter 4: An Architecture to Suit You\ 75

A Measuring Stick 75 Prerequisites\ 76 Model View ViewModel (MVVM)\ 77 Model 78 View 79 ViewModel\ 80 Model View Update (MVU)\ 83 Getting Started with Comet\ 84 Adding Your MVU Implementation\ 84

v

Table of Contents

XAML vs. C# Markup\ 86 Plain C#\ 87 C# Markup\ 88 Chosen Architecture for This Book 89 Adding the ViewModels\ 90 Adding Views\ 96 Viewing Your Widget\ 99 MVVM Enhancements 101 Summary\ 105 Source Code\ 107

Part II: User Interface\ 109

Chapter 5: User Interface Essentials\ 111

Prerequisites\ 111

Models 111

Pages 113

ViewModels\ 114

App Icons\ 116

Adding Your Own Icon 116

Platform Differences 117

Splash Screen\ 118

XAML 119

Dissecting a XAML File\ 120

Building Your First XAML Page\ 122

Layouts\ 124

AbsoluteLayout\ 124

FlexLayout\ 126

Grid\ 128

HorizontalStackLayout 131

vi

Table of Contents

VerticalStackLayout 132 Data Binding\ 135 Binding\ 136 Applying the Remaining Bindings 140 MultiBinding\ 141 Command\ 144 Compiled Bindings 147 Shell\ 148 ShellContent\ 149 Navigation\ 150 Flyout 154 Tabs\ 160 Search\ 160 Taking Your Application for a Spin 161 Summary\ 163 Source Code\ 164 Extra Assignment\ 164

Chapter 6: Creating Our Own Layout\ 165

Placeholder\ 166 ILayoutManager\ 168 BoardLayout\ 169 BoardLayout.xaml 169 BoardLayout.xaml.cs\ 172 FixedLayoutManager 177 Accepting the Number of Rows and Columns for a Board\ 179 Providing Tap/Click Support Through a Command\ 181 Building the Board Layout\ 182 Setting the Correct Row/Column Position for Each Widget\ 185

vii

Table of Contents

Using Your Layout\ 187

Adding a Factory That Will Create Instances of Your Widgets\ 187

WidgetTemplateSelector 193

Updating FixedBoardPageViewModel 194

Finally Using the Layout\ 196

Summary\ 197

Source Code\ 198

Extra Assignment\ 198

Chapter 7: Accessibility 199

What Is Accessibility?\ 199 Why Make Your Applications Accessible?\ 200 What to Consider When Making Your Applications Accessible 200 How to Make Your Application Accessible\ 201 Screen Reader Support\ 201 Suitable Contrast\ 208 Dynamic Text Sizing 210 Testing Your Application’s Accessibility 215 Android\ 215 iOS\ 215 macOS\ 216 Windows\ 216 Accessibility Checklist\ 216 Summary\ 218 Source Code\ 219 Extra Assignment\ 219

viii

Table of Contents

Chapter 8: Advanced UI Concepts 221

Adding the Ability to Add a Widget to a Board 221

Possible Ways of Achieving Your Goal\ 222

The Chosen Approach 224

Styling\ 230

Examining the Default Styles 232

Creating a Style\ 234

AppThemeBinding\ 236

Further Reading 236

Triggers\ 237

Creating a DataTrigger 238

EnterActions and ExitActions 239

Creating a TriggerAction\ 240

Further Reading 242

Animations 242

Basic Animations\ 243

Combining Basic Animations\ 245

Cancelling Animations\ 246

Easings\ 246

Complex Animations\ 247

Combining Triggers and Animations\ 252

Summary\ 254

Source Code\ 255

Extra Assignment\ 255

Animate the BoxView Overlay 255

Animate the New Widget\ 255

ix

Table of Contents

Part III: Behind the Scenes\ 257

Chapter 9: Local Data\ 259

What Is Local Data?\ 259 File System\ 260 Cache Directory\ 260 App Data Directory\ 261 Database\ 261 Repository Pattern 262 SQLite\ 270 LiteDB\ 278 Database Summary\ 285 Application Settings (Preferences) 285 What Can Be Stored in Preferences?\ 286 Setting a Value in Preferences\ 286 Getting a Value in Preferences\ 288 Checking if a Key Exists in Preferences\ 290 Removing a Preference\ 291 Secure Storage\ 291 Storing a Value Securely 291 Reading a Secure Value 292 Removing a Secure Value\ 292 Platform specifics 293 Viewing the Result\ 295 Summary\ 296 Source Code\ 297 Extra Assignment\ 297

x

Table of Contents

Chapter 10: Remote Data 299

What Is Remote Data?\ 299 Considerations When Handling Remote Data\ 300 Webservices 302 The Open Weather API\ 302 Adding Some State\ 319 Simplifying Webservice Access\ 326 Prebuilt Libraries\ 327 Code Generation Libraries\ 327 Further Reading\ 329 Polly 329 StateContainer from CommunityToolkit.Maui 330 Summary\ 330 Source Code\ 331 Extra Assignment\ 331 TODO Widget 331 Quote of the Day Widget\ 332 NASA Space Image of the Day Widget 332

Part IV: Utilizing the platforms\ 333

Chapter 11: Getting Specific\ 335

.NET MAUI Essentials\ 335

Permissions 336

Using the Geolocation API 341

Configuring Platform-Specific Components\ 346

Platform-Specific API Access 352

Platform-Specific Code with Compiler Directives\ 352

Platform-Specific Code in Platform Folders\ 354

xi

Table of Contents

Overriding the Platform-Specific UI\ 355

OnPlatform\ 355

Handlers\ 358

Summary\ 361

Source Code\ 362

Extra Assignment\ 362

Barometer Widget 362

Geocoding Lookup\ 363

Chapter 12: Testing 365

Unit Testing\ 365 Unit Testing in .NET MAUI\ 366 Adding Your Own Unit Tests 368 Testing Your View Models\ 372 Testing Asynchronous Operations\ 374 Testing Your Views 380

Device Testing\ 382 Creating a Device Test Project 383 Adding a Device-Specific Test\ 383 Running Device-Specific Tests\ 384 Snapshot Testing 386 Snapshot Testing Your Application 387 Passing Thoughts\ 390 Looking to the Future 390 Summary\ 391 Source Code\ 391

xii

Table of Contents

Chapter 13: Lets Get Graphical\ 393

.NET MAUI Graphics\ 393 Drawing on the Screen\ 394 Further Reading 397 Building a Sketch Widget\ 397 Creating the SketchWidgetViewModel\ 397 Representing a User Interaction\ 398 Creating the SketchWidgetView\ 399 Registering Your Widget\ 404 Taking Your Widget for a Test Draw\ 404 Summary\ 405 Source Code\ 405 Extra Assignment\ 406

Part V: Finishing Our Application 407

Chapter 14: Releasing Our Application 409

Distributing Your Application\ 409 Android\ 410 iOS\ 412 macOS\ 415 Windows\ 416

Things to Consider\ 416 Following Good Practices\ 416 Performance 418 Linking 421 Crashes/Analytics 422 Obfuscation\ 424

Distributing Test Versions 426 Summary\ 427 xiii

Table of Contents

Chapter 15: Conclusion 429

Looking at the Final Product\ 429

Taking the Project Further\ 431

Useful Resources\ 432

StackOverflow\ 433

GitHub 433

YouTube\ 433

Social Media\ 434

Yet More Goodness 434

Looking Forward\ 434

Upgrading from Xamarin.Forms\ 435

Comet\ 435

Testing\ 436

Index 437

xiv