Alessandro Del Sole

Beginning Visual Studio for Mac

Build Cross-Platform Apps with Xamarin and .NET Core

Alessandro Del Sole

Cremona, Italy

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/9781484230329 . For more detailed information, please visit www.apress.com/source-code .

ISBN 978-1-4842-3032-9

e-ISBN 978-1-4842-3033-6

https://doi.org/10.1007/978-1-4842-3033-6

Library of Congress Control Number: 2017957992

© Alessandro Del Sole 2017

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.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], 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.

To my father, the brave, strong man I always hope to be like.

To Angelica, who brings the sunshine into my life every day.

To my mother, I can feel you’re still around.

Introduction

In recent years, the world of software development has changed a lot and for many reasons. Among others, the large diffusion of mobile devices with different operating systems and of cloud platforms and services has had a huge impact on the way developers write code and what they write code for. If you think about how software development was just a few years ago, you will recognize some even bigger changes.

In fact, in the past, if you wanted (or needed) to build applications for different operating systems, platforms, or devices, you had to use proprietary development tools and native frameworks on each specific platform. For example, Microsoft Visual Studio has always been the development environment of choice if you wanted to build Windows and web applications based on the .NET Framework with C#, F#, Visual Basic, and C++. If you are like me and you have a lot of experience with .NET on Windows, you know how powerful the .NET technology is. However, the limitation is that it runs only on Windows, which means it requires you to publish your .NET web apps and services only to Windows Server systems, while many companies actually want Linux as the host. Additionally, developers working on the Apple stack do not have a framework available that allows for building web apps and services at all. As another example, building mobile apps for Android, iOS, and Windows 10 requires knowledge of native frameworks, development tools, and languages, such as Java and Google Android Studio for Android, Apple Xcode and Swift or Objective-C for iOS, and Microsoft Visual Studio and C# (or Visual Basic) for Windows 10. As an implication, the effort your company might need to make to publish an app on all the major stores can be huge and might involve hiring several specialized developers to reduce the development time, which will mean higher costs. Or this might mean waiting for the current developers to acquire the skills and experience they need to build apps for different systems, which might save some money but requires much more time. And you know that time is money. The list of examples might be longer and involve other technologies, such as cloud platforms and containers, but these two are enough to give you an idea of what software development was at a certain point in time. Concurrently, the demand for applications and services to be available on multiple platforms has increased in the market. In summary, many companies have been in a situation in which they needed to be on multiple platforms but with either limited resources or a very big effort.

Microsoft was aware of all the aforementioned problems, and in the last years the company has significantly changed its vision and strategy, opening up to other platforms, embracing open source, and focusing even more on cloud services. In fact, Microsoft has been making huge investments in bringing technologies, platforms, developer tools, frameworks, and services to other operating systems such as Linux and macOS and to typically non-Microsoft audiences by focusing on services much more than in the past. In this strategy, cross-platform development is of primary importance and relies on two major technologies (apart from cloud services and platforms).

  • .NET Core, a modular, open source, cross-platform subset of the .NET Framework that enables developers to write applications and services that run on Windows, Linux, and Mac in C# and F#

  • Xamarin, a technology that allows developers to write mobile applications that run on Android, iOS, and Windows with C# and F#

In this vision, there is one important pillar: using .NET languages such as C# and F# to write code for any platform. The most important thing is that developers can reuse their .NET skills on Linux and macOS, not just Windows. But to make this possible, developers need professional tools they can use to create the next generation of application. Those working on Windows have Visual Studio 2017, the most powerful version ever of the premiere development environment from Microsoft, which now supports a variety of non-Microsoft technologies. For other systems, Microsoft released Visual Studio Code ( http://code.visualstudio.com ), an open source, cross-platform tool that provides an enhanced coding experience. However, Visual Studio Code has no built-in designer tools, has no support for Xamarin (at least currently), and does not provide specialized tools that developers might need, for example, integrated publishing tools or profiling instruments. After all, its focus is on providing an evolved editing experience. With its history and tradition of delivering the best developer tools in the world and because you need a Mac to build, sign, and distribute apps for iOS and macOS, Microsoft finally released Visual Studio 2017 for Mac (in May 2017), an integrated development environment that can be used on macOS to build apps that run on any platform and any device in C# and F# and that perfectly fits into this mobile-first, cloud-first vision. Visual Studio 2017 for Mac is neither a simple porting of Visual Studio on Windows nor is it an evolution of Visual Studio Code. Instead, it is a professional development environment specifically built for the Mac, with native user interface and tools tailored for macOS and for cross-platform development.

This book provides a comprehensive guide to Visual Studio 2017 for Mac, paying particular attention to the integrated development environment, the workspace, and all the integrated tools you can leverage to build high-quality, professional applications for mobile devices and the Web, using C# as the programming language of choice. A basic knowledge of C# is strongly recommended to get the most out of the book because it is not possible to concurrently teach the language and the Visual Studio for Mac environment in one book. Therefore, I will assume you are familiar with the syntax and with the most common constructs. You will also find an introduction to the Xamarin and .NET Core technologies so that you will learn the necessary foundations to get started. Then you will be able to separately deep-dive into both technologies. Actually, with Visual Studio for Mac, you can also develop games based on Unity ( http://unity3d.com ). Game development with Unity will not be covered in this book, but it is worth mentioning that Visual Studio for Mac already includes the tools for Unity out of the box, and you only have to install the Unity engine separately.

Visual Studio 2017 for Mac is available in three different editions: Community, Professional, and Enterprise. If you do not have an MSDN subscription and you are an individual developer, you can install the Community edition for free from http://bit.ly/2tsuJvR . This edition can also be used under strict licensing terms in both enterprise and nonenterprise organizations. Microsoft has a specific page that describes the license for Visual Studio 2017 for Mac Community, available at www.visualstudio.com/license-terms/mlt553321 .

Unless expressly specified, all the topics described in this book are available in all three editions. Regarding system requirements, you will need a Mac with at least 4GB RAM, an 1.8GHz processor, and 1GB of disk space. El Capitan 10.11 is the minimum version of macOS that supports Visual Studio for Mac. Of course, the recommended configuration is with macOS Sierra 10.12 and with 8GB RAM. Just to give you an idea, the machine I used to write this book is a Mac Mini 2014 with 8GB RAM.

After this short introduction, it is time to get started. But before you can put your hands on Visual Studio for Mac, you need to set up and configure your Mac machine for development. That is what Chapter 1 is going to explain.

Acknowledgments

Writing books is hard work, not only for the author but also for all the people involved in the reviews and in the production process.

Therefore, I would like to thank Joan Murray, Jill Balzano, Laura Berendson, and everyone at Apress who contributed to publishing this book and made the process much more pleasant.

A very special thanks to Jordan Matthiesen and the people on the Visual Studio for Mac team at Microsoft who have tech-edited this book. These folks did an incredible job walking through every single sentence and every single line of code, and their contributions were invaluable to the creation of this book.

I would also like to thank the Technical Evangelism team of the Italian subsidiary of Microsoft and my Microsoft MVP lead, Cristina G. Herrero, for their continuous support and encouragement for my activities.

As the community leader of the Italian Visual Studio Tips & Tricks community ( www.visualstudiotips.net ), I want to say “thank you!” to the other team members (Laura La Manna, Renato Marzaro, Antonio Catucci, and Igor Damiani) and to our followers for keeping strong our passion for sharing knowledge and helping people solve problems in their daily work.

Thanks to my everyday friends, who are always ready to encourage me even if they are not developers.

Finally, special thanks to my girlfriend, Angelica, who knows how strong my passion for technology is and who never complains about the time I spend writing.

Contents

  1. Part I: Preparing for Development
    1. Chapter 1:​ Configuring the Mac Development Machine
      1. Getting an Apple ID
      2. Upgrading macOS
      3. Creating a Developer Account
      4. Installing and Configuring Xcode
        1. Installing Xcode
        2. Configuring the Developer Profile
      5. Installing and Configuring Visual Studio for Mac
      6. Summary
    2. Chapter 2:​ Getting Started with the IDE and with Projects
      1. Taking a Step Back in History:​ Xamarin Studio
      2. Looking at the Welcome Page
        1. The Get Started Area
        2. The Recent Area
        3. The News Area
      3. Understanding Runtimes and SDKs
      4. Working with Projects
        1. Creating Projects
        2. Creating Your First C# Project
        3. Building and Running an Application
        4. Adding and Managing Dependencies
        5. Configuring Project Options
        6. Adding, Removing, and Renaming Items
      5. Working with Pads
        1. Docking, Hiding, and Rearranging Pads
        2. The Classes Pad
        3. The Errors Pad
        4. The Properties Pad
        5. The Tasks Pad
        6. The Package Console Pad
      6. Working with the Code Editor
        1. Using Syntax Colorization
        2. Using the Edit Menu
        3. Zooming the Code Editor
        4. Fast Coding with IntelliSense
        5. Detecting and Fixing Code Issues As You Type
        6. Refactoring Your Code
        7. Navigating Your Code
        8. Generating Types on the Fly
        9. Enabling Multicursors
      7. Applying Themes
      8. Using the Navigate To Tool
      9. Browsing Help and Documentation
        1. The Help Pad
        2. The Help Menu
      10. Summary
    3. Chapter 3:​ Debugging Applications
      1. Meeting the Debuggers
      2. Preparing an Example
      3. Debugging an Application
      4. Using Breakpoints and Data Visualizers
        1. Configuring Breakpoints with Settings and Actions
      5. Investigating Runtime Errors
      6. Debugging Instrumentation
        1. Debugging in Steps
        2. Debugging Pads
      7. Debugging in Code
      8. Summary
  2. Part II: Building Mobile Apps with Xamarin
    1. Chapter 4:​ Introducing Xamarin
      1. Considerations About Cross-Platform App Development
      2. Understanding Xamarin and Its Objectives
        1. The Xamarin Platform
        2. The Visual Studio Mobile Center
        3. The Xamarin University
      3. Summary
    2. Chapter 5:​ Building Android Applications
      1. Preparing a Device for Development
      2. Creating an Android App with Xamarin
        1. Understanding Fundamental Concepts About Xamarin.​Android Solutions
        2. Understanding References and NuGet Packages
        3. Using Components
        4. Designing the User Interface
        5. Accessing the Android APIs in C#
        6. Setting App Properties with the Android Manifest
        7. Building and Debugging Apps
        8. Managing Emulators and SDKs
      3. Preparing Apps for Publishing
      4. Summary
    3. Chapter 6:​ Building iOS Applications
      1. Creating an iOS App with Xamarin
        1. Understanding Fundamental Concepts About Xamarin.​iOS Solutions
        2. Understanding References and NuGet Packages
        3. Using Components
        4. Designing the User Interface
        5. Accessing the Apple APIs in C#
        6. Setting App Properties and Permissions
        7. Building and Debugging Apps
      2. Preparing Apps for Publishing
      3. Hints on macOS and tvOS Development
      4. Summary
    4. Chapter 7:​ Building Cross-Platform Apps with Xamarin.​Forms
      1. Introducing Xamarin.​Forms
      2. Creating and Understanding Xamarin.​Forms Solutions
        1. Understanding the Structure of Xamarin.​Forms Solutions
        2. Understanding the Architecture of the Sample Application
        3. Managing the Application Life Cycle
        4. Designing the User Interface with XAML
        5. Accessing Shared APIs in C#
      3. Building and Debugging Applications
        1. XAML Compilation
        2. Configuring PCL Project Options
        3. Moving from PCLs to .​NET Standard Libraries
        4. Analyzing Applications with Instrumentation
      4. Summary
    5. Chapter 8:​ Platform-Specific Code in Xamarin.​Forms
      1. Using the Dependency Service
        1. Adding the SQLite NuGet Packages
        2. Implementing the Dependency Service
      2. Using Plug-ins
      3. Using Native Visual Elements
        1. Adding Native Views to XAML
        2. Introducing Custom Renderers
        3. Introducing Effects
      4. Summary
  3. Part III: Building Cross-platform Applications with .NET Core
    1. Chapter 9:​ Introducing .​NET Core
      1. What Is .​NET Core?​
        1. Understanding the Architecture of .​NET Core
        2. Getting Started with the Command-Line Tools
        3. Understanding the Relationship Between .​NET Core with .​NET Standard
      2. Summary
    2. Chapter 10:​ Creating ASP.​NET Core Web Applications
      1. What Is ASP.​NET Core?​
      2. What Is MVC?​
      3. Creating an ASP.​NET Core Web Application
        1. Understanding ASP.​NET Core Solutions
        2. The Model:​ Working with Entity Framework Core
        3. Working with Controllers
        4. Working with Views
      4. Running, Debugging, and Testing ASP.​NET Core Web Applications
      5. Publishing ASP.​NET Core Web Applications
      6. Summary
    3. Chapter 11:​ Creating RESTful Web API Services
      1. Creating ASP.​NET Core Web API Services
        1. Creating a Model
        2. Adding Entity Framework Core
        3. Implementing Controllers
        4. Hints About Identity
      2. Debugging and Testing Web API Services
      3. Publishing ASP.​NET Core Web API Services
      4. Consuming Services:​ A Xamarin.​Forms Client
        1. Debugging Multiple Projects
      5. Summary
  4. Part IV: Collaboration and Productivity
    1. Chapter 12:​ Team Collaboration:​ Version Control with Git
      1. What Is Git?​
        1. Preparing an Example
      2. Creating a Remote Repository
      3. Initializing a Local Repository
      4. Cloning Repositories
      5. Managing File Changes
      6. Managing Commits
        1. Hints About Stashes
      7. Managing Branches
        1. Merging Branches
        2. Branching Strategies
      8. Summary
    2. Chapter 13:​ A Deeper Look at the IDE
      1. Customizing Visual Studio
        1. Customizing Key Bindings
        2. Customizing Fonts
        3. Customizing the Code Editor
        4. Customizing Options for Projects and Solutions
        5. Saving and Restoring Layouts
      2. Extending Visual Studio
        1. Hints About Extension Authoring
      3. Improving the Code Editing Experience
        1. Working with Code Snippets
        2. Customizing Source Code Styles
      4. Getting into Advanced NuGet Management
        1. Customizing NuGet
        2. Adding and Creating Repositories
      5. Using Other Integrated Tools
        1. Using the Assembly Browser
        2. Using the Regular Expressions Toolkit
        3. Adding Custom Tools
      6. Consuming Connected Services
      7. Summary
  5. Index

About the Author and About the Technical Reviewers

About the Author

A449551_1_En_BookFrontmatter_Figb_HTML.jpg

Alessandro Del Sole has been a Microsoft Most Valuable Professional (MVP) since 2008, and he is a Xamarin Certified Mobile Developer and Microsoft Certified Professional. Awarded MVP of the Year in 2009, 2010, 2011, 2012, and 2014, he is internationally considered a Visual Studio expert and a .NET authority. He has authored many books on programming with Visual Studio, Xamarin, and .NET, and he has blogged and written numerous technical articles about Microsoft developer topics in Italian and English for many developer sites, including MSDN Magazine and the Visual Basic Developer Center from Microsoft. He is a frequent speaker at Microsoft technical conferences, and his Twitter alias is @progalex .

About the Technical Reviewers

Jordan Matthiesen is a motivated and personable technology leader with nearly 14 years of experience successfully delivering technology solutions for product companies, marketing teams, and governments. He is an expert in web solutions architecture and development, using tools and techniques such as Microsoft .NET, HTML5, JavaScript, Ajax, high-performance web site design, test-driven development, unit testing, and Microsoft SQL Server. Jordan’s specialties include solutions architecture, Microsoft .NET, team leadership, project management, and software development.

A449551_1_En_BookFrontmatter_Figc_HTML.jpg

Mathieu Clerici is the CEO and lead Xamarin architect at Los Xamarinos, a Xamarin consultancy company. He has been a .NET developer since 2009 and has been focused on Xamarin development since 2013. He frequently gives speeches at monthly .NET mobile meetups in Guadalajara about Xamarin technology and contributes to open source Xamarin plug-ins in his spare time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.162.130