Xamarin.iOS for Visual Studio 简介Introduction to Xamarin.iOS for Visual Studio

借助 Xamarin for Windows,iOS 应用程序可在 Visual Studio 中进行编写和测试,并且由联网的 Mac 提供生成和部署服务。Xamarin for Windows allows iOS applications to be written and tested within Visual Studio, with a networked Mac providing the build and deployment service.

本文介绍在每台计算机上安装和配置 Xamarin.iOS 工具以使用 Visual Studio 生成 iOS 应用程序的步骤。This article covers the steps to install and configure the Xamarin.iOS tools on each computer to build iOS applications using Visual Studio.

在 Visual Studio 内部开发 iOS 具有以下优点:Developing for iOS inside Visual Studio provides a number of benefits:

  • 为 iOS、Android 和 Windows 应用程序创建跨平台解决方案。Creation of cross-platform solutions for iOS, Android and Windows applications.
  • 对所有跨平台项目(包括 iOS 源代码)使用常用 Visual Studio 工具(如 Resharper 和 Team Foundation Server)。Using your favorite Visual Studio tools (such as Resharper and Team Foundation Server) for all your cross-platform projects, including iOS source code.
  • 使用熟悉的 IDE,同时利用所有 Apple API 的 Xamarin.iOS 绑定。Work with a familiar IDE, while taking advantage of Xamarin.iOS bindings of all Apple’s APIs.

要求和安装Requirements and Installation

在 Visual Studio 中开发 iOS 时必须遵守一些要求。There are a few requirements that must be adhered to when developing for iOS in Visual Studio. 正如概述中简要提到,编译 IPA 文件需要 Mac,且应用程序不能部署到没有 Apple 证书和代码签名工具的设备上。As briefly mentioned in the overview, a Mac is required to compile IPA files, and applications cannot be deployed to a device without Apple’s certificates and code-signing tools.

可从众多配置选项中选择最适合开发需求的选项。There are a number of configuration options available, so you can decide which works best for your development needs. 以下列出了这些选项:These are listed below:

  • 使用 Mac 作为主要的开发计算机,并运行安装了 Visual Studio 的 Windows 虚拟机。Use a Mac as your main development machine and run a Windows virtual machine with Visual Studio installed. 建议使用 VM 软件,如 ParallelsVMWareWe recommend using VM software such as Parallels or VMWare .
  • 将 Mac 当做生成主机使用。Use a Mac just as a build host. 在这种情况下,需连接到与 Windows 计算机所处的同一网络上,该计算机已安装必备工具。In this scenario it would be connected to the same network as a Windows machine with the necessary tools installed.

对于每种情况,都应遵循以下步骤:In either case, you should follow these steps:

连接到 MacConnecting to the Mac

要将 Visual Studio 连接到 Mac 生成主机,请按照与 Mac 配对指南中的说明进行操作。To connect Visual Studio to your Mac build host, follow the instructions in the Pair to Mac guide.

Visual Studio 工具栏概述Visual Studio Toolbar Overview

Xamarin iOS for Visual Studio 将项添加到标准工具栏和新的 iOS 工具栏。Xamarin iOS for Visual Studio adds items to the Standard toolbar and to the new iOS toolbar. 下面介绍了这些工具栏的功能。The functions of these toolbars are explained below.

标准工具栏Standard Toolbar

与 Xamarin iOS 开发相关的控件用红线圈出:The controls relevant to Xamarin iOS development are circled in red:

  • 开始 - 开始在选定平台上调试或运行应用。Start - Starts debugging or running the application on the selected platform. Mac 必须已连接(可查看 iOS 工具栏中的状态指示器)。There must be a connected Mac (see the status indicator in the iOS toolbar).
  • 解决方案配置 - 允许选择要使用的配置(例如,调试和发布)。Solution Configurations – Allows you to select the configuration to use (e.g., Debug, Release).
  • 解决方案平台 - 可以选择“iPhone”或“iPhoneSimulator”进行部署。Solution Platforms - Allows you to select iPhone or iPhoneSimulator for deployment.

iOS 工具栏iOS Toolbar

iOS 工具栏在所有版本的 Visual Studio 中都类似。The iOS Toolbar in Visual Studio looks similar in each version of Visual Studio. 外观如下所示:These are all shown below:

下面解释每个项:Each item is explained below:

  • Mac 代理/连接管理器 - 显示 Xamarin Mac 代理对话框。Mac Agent/Connection Manager – Displays the Xamarin Mac Agent dialog box. 正在连接时,此图标显示为橙色;已连接时,显示为绿色。This icon will appear orange when connecting, and green when connected.
  • 显示 iOS 模拟器 - 在 Mac 上将 iOS 模拟器窗口放在最前面。Show iOS Simulator – Brings the iOS Simulator window to the front on the Mac.
  • 显示生成服务器上的 IPA 文件 - 打开 Mac 上的 Finder,找到应用程序的 IPA 输出文件位置。Show IPA File on Build Server – Opens Finder on the Mac to the location of the application’s IPA output file.

iOS 输出选项iOS Output Options

输出窗口Output Window

“输出”窗格具有多个选项,可以查看这些选项,发现有关生成、部署、连接消息和错误。There are options in the Output pane that you can view to discover build, deployment, and connection messages and errors.

以下屏幕截图显示可用的输出窗口,这些窗口可能因项目类型而有所不同:The screenshot below shows the available output windows, which may differ depending on your project type:

  • Xamarin - 此项包含仅与 Xamarin 相关的信息,如与 Mac 的连接情况和激活状态。Xamarin – This contains information relating solely to Xamarin, such as the connection to the Mac and activation status.

  • Xamarin 诊断 - 此窗格显示 Xamarin 项目的更多详细信息,如与 Android 的交互。Xamarin Diagnostics – This shows more detailed information about your Xamarin project, such as the interaction with and for Android.

仍可以在“输出”视图中选择其他默认 Visual Studio 输出窗格(如“调试”和“生成”),以便调试输出和 MSBuild 输出:Other default Visual Studio Output panes like Debug and Build are still available inside the Output view and are used for Debugging Output and MSBuild Output:

  • 调试Debug

  • 生成生成顺序Build & Build Order

iOS 项目属性iOS Project Properties

可通过右键单击项目名称并选择上下文菜单中的“属性”找到 Visual Studio 的项目属性。Visual Studio’s Project Properties can be accessed by right-clicking on the Project name and selecting Properties in the context menu. 这样就可以配置 iOS 应用程序了,如以下屏幕截图所示:This will let you configure your iOS application, as shown in the screenshot below:

  • iOS 应用程序包签名 - 连接 Mac 以填充代码签名标识和预配配置文件:iOS Bundle Signing – connects to the Mac to populate the code signing identities and provisioning profiles:

    填充代码签名标识和预配配置文件

  • iOS IPA 选项 - IPA 文件将保存到 Mac 文件系统中:iOS IPA Options – the IPA file will be saved on the Mac’s file system:

    iOS IPA 选项

  • iOS 运行选项 - 配置其他参数:iOS Run Options – Configure additional parameters:

    iOS 运行选项

为 iOS 应用程序创建新的项目Creating a New Project for iOS Applications

在 Visual Studio 中创建新的 iOS 项目同创建其他项目类型一样。Creating a new iOS project from within Visual Studio is done just like any other project type. 选择“文件”>“新建项目”会打开如下所示的对话框,描述创建新的 iOS 项目的一些可用项目类型:Selecting File > New Project will open the dialog shown below, illustrating some of the project types available for creating a new iOS project:

创建新项目

选择“iOS 应用(Xamarin)”会显示如下所示的模板,用于创建新的 Xamarin.iOS 应用程序:Selecting iOS App (Xamarin) will show the following templates for creating a new Xamarin.iOS application:

选择 iOS 应用的模板

可以在 Visual Studio 中使用 iOS 设计器编辑情节提要和 .xib 文件。Storyboard and .xib files can be edited in Visual Studio using the iOS Designer. 若要创建情节提要,请选择某个情节提要模板。To create a Storyboard, choose the one of the Storyboard templates. 这会在“解决方案资源管理器”中生成 Main.storyboard 文件,如以下屏幕截图所示:This will generate a Main.storyboard file in the Solution Explorer as illustrated by the screenshot below:

解决方案资源管理器中的 Main.storyboard 文件

若要开始创建或编辑情节提要,请双击 Main.storyboard 将其在 iOS 设计器中打开:To start creating or editing your Storyboard, double-click on Main.storyboard to open it in the iOS Designer:

若要向视图添加对象,请使用“工具箱”窗格,将项拖放到设计图面上。To add objects to your view, use the Toolbox pane to drag and drop items onto your Design Surface. 如果尚未添加工具箱,可通过选择“视图”>“工具箱”添加。The Toolbox can be added by selecting View > Toolbox, if not already added. 可使用“属性”窗格修改对象属性、调整布局,并创建事件,如下所示:Object properties can be modified, their layouts adjusted, and events can be created by using the Properties pane, as illustrated below:

有关使用 iOS 设计器的详细信息,请参阅设计器指南。For more information on using the iOS designer, refer to the Designer guides.

运行和调试 iOS 应用程序Running & Debugging iOS Applications

设备日志记录Device Logging

在 Visual Studio 2017 中,统一了 Android 和 iOS 日志面板。In Visual Studio 2017, the Android and iOS log pads are unified.

Visual Studio 新的“设备日志”工具窗口允许显示 Android 和 iOS 设备的日志。The new Device Log tool window for Visual Studio allows to show logs for Android and iOS devices. 执行以下任一命令均可显示此窗口:It can be shown by executing any of the following commands:

  • “视图”>“其他窗口”>“设备日志”View > Other Windows > Device Log
  • “工具”>“iOS”>“设备日志”Tools > iOS > Device Log
  • “iOS 工具栏”>“设备日志”iOS toolbar > Device Log

在工具窗口显示后,用户便可以从“设备”下拉列表中选择物理设备。Once the tool window is shown, the user can select the physical device from the devices dropdown. 选定设备后,日志将自动添加到表中。When a device is selected, logs will automatically be added to the table. 在设备之间切换可停止和启动设备日志记录。Switching between devices will stop and start the device logging.

为了在组合框中显示设备,必须加载 iOS 项目。In order for the devices to appear in the combobox, an iOS project must be loaded. 此外对于 iOS,Visual Studio 还必须连接到 Mac 服务器以发现连接到 Mac 的 iOS 设备。Additionally for iOS, Visual Studio must be connected to the Mac Server to discover the iOS devices connected to the Mac.

此工具窗口包括:一个日志条目表、一个用于选择设备的下拉列表、一种清除日志条目的方法、一个搜索框,以及播放/停止/暂停按钮。This tool window provides: a table of log entries, a dropdown for device selection, a way to clear log entries, a search box, and play/stop/pause buttons.

设置调试停止Set Debugging Stops

可以在应用程序中的任意位置设置断点,将信号发送到调试器以暂停程序执行。Breakpoints can be set at any point in your application to signal to the debugger to temporarily stop the execution of the program. 若要在 Visual Studio 中设置断点,请在想要中断的代码行号旁边,单击编辑器的空白区:To set a breakpoint in your Visual Studio, click on the margin area of your editor, next to the line number of the code you wish to break at:

开始调试,并使用模拟器或设备将应用程序导航到断点。Start debugging, and use the simulator or device to navigate your application to a breakpoint. 命中断点时,将突出显示此行并启用 Visual Studio 的正常调试行为:你可以进入、结束或跳出代码,检查局部变量,或使用即时窗口。When a breakpoint is hit, the line will be highlighted and Visual Studio’s normal debugging behavior will be enabled: you can step into, over, or out of the code, examine local variables, or use the Immediate Window.

此屏幕截图显示使用 macOS 上的 Parallels 在 Visual Studio 旁运行的 iOS 模拟器:This screenshot shows the iOS Simulator running next to Visual Studio using Parallels on macOS:

此屏幕截图显示使用 macOS 上的 Parallels 在 Visual Studio 旁运行的 iOS 模拟器

检查局部变量Examine Local Variables

通过调试检查局部变量

总结Summary

本文介绍如何使用 Xamarin.iOS for Visual Studio。This article described how to use Xamarin iOS for Visual Studio. 列出了可用于从 Visual Studio 中创建、生成和测试 iOS 应用的各种功能,并介绍了如何生成和调试简单的 iOS 应用程序。It listed the various features available for creating, building, and testing an iOS app from within Visual Studio and walked through building and debugging a simple iOS application.