Visual Studio 中的跨平台行動裝置應用程式開發Cross-platform mobile development in Visual Studio

您可以使用 Visual Studio 建置 Android、iOS 和 Windows 裝置的應用程式。You can build apps for Android, iOS, and Windows devices by using Visual Studio. 設計應用程式時,請使用 Visual Studio 中的工具,輕鬆地加入已連線的服務,例如 Office 365、Azure App Service 和 Application Insights。As you design your app, use tools in Visual Studio to easily add connected services such as Office 365, Azure App Service, and Application Insights.

使用 C# 和 .NET Framework、HTML 和 JavaScript 或 C++ 建置您的應用程式。Build your apps by using C# and the .NET Framework, HTML and JavaScript, or C++. 共用程式碼、字串、影像,甚至在某些情況下,共用使用者介面。Share code, strings, images, and in some cases even the user interface.

如果您要建置遊戲或沈浸式圖形化應用程式,請安裝 Visual Studio Tools for Unity,並享受搭配 Unity 提供的各種功能強大且具生產力的 Visual Studio 功能,這個熱門的跨平台遊戲/圖形引擎與開發環境適用於在 iOS、Android、Windows 及其他平台上執行的應用程式。If you want to build a game or immersive graphical app, install Visual Studio tools for Unity and enjoy all of the powerful productivity features of Visual Studio with Unity, the popular cross-platform game/graphics engine and development environment for apps that run on iOS, Android, Windows, and other platforms.

建置 Android、iOS 和 Windows 的應用程式 (.NET Framework)Build an app for Android, iOS, and Windows (.NET Framework)

裝置Devices

使用 Visual Studio Tools for Xamarin,您可以在同一個解決方案中將目標設為 Android、iOS 及 Windows,來共用程式碼,甚至共用 UI。With Visual Studio Tools for Xamarin, you can target Android, iOS, and Windows in the same solution, sharing code and even UI.

進一步了解Learn more
安裝 Visual Studio (VisualStudio.com)Install Visual Studio (VisualStudio.com)
了解 Visual Studio 中的 Xamarin (VisualStudio.com)Learn about Xamarin in Visual Studio (VisualStudio.com)
Xamarin 行動應用程式開發文件Xamarin mobile app development documentation
使用 Xamarin 應用程式進行 DevOpsDevOps with Xamarin apps
了解 Visual Studio 的通用 Windows app (VisualStudio.com)Learn about Universal Windows apps in Visual Studio (VisualStudio.com)
了解 Swift 與 C# 之間的相似性 (download.microsoft.com)Learn about the similarities between Swift and C# (download.microsoft.com)

以 Android、iOS 和 Windows 為目標的單一程式碼基底Target Android, iOS, and Windows from a single code base

您可以使用C#或F# ,建立適用于 Android、iOS 和 Windows 的原生應用程式(目前不支援 Visual Basic)。You can build native apps for Android, iOS, and Windows by using C# or F# (Visual Basic is not supported at this time). 若要開始使用,請安裝 Visual Studio,請在安裝程式中選取 [使用 .net 進行行動開發] 選項。To get started, install Visual Studio, select the Mobile Development with .NET option in the installer.

如果您已經安裝 Visual Studio,請重新執行Visual Studio 安裝程式,然後選取適用于 Xamarin 的相同 [使用 .Net 進行行動開發] 選項(如上所示)。If you already have Visual Studio installed, re-run the Visual Studio Installer and select the same Mobile Development with .NET option for Xamarin (as above).

當您完成時,專案範本會出現在 [新增專案] 對話方塊中。When you're done, project templates appear in the New Project dialog box. 尋找 Xamarin 範本的最簡單方式是只需搜尋「Xamarin」。The easiest way to find Xamarin templates is to just search on "Xamarin."

Xamarin 會以 .NET 類別和方法形式公開 Android、iOS 和 Windows 的原生功能。Xamarin exposes the native functionality of Android, iOS, and Windows as .NET classes and methods. 這表示您的應用程式具備原生 API 和原生控制項的完整存取權;而且,它們與以原生平台語言撰寫的應用程式一樣回應靈敏。This means your apps have full access to native APIs and native controls, and they're just as responsive as apps written in the native platform languages.

建立專案之後,您將可以利用 Visual Studio 的所有生產力功能。After you create a project, you'll leverage all of the productivity features of Visual Studio. 例如,您將使用設計工具來建立頁面,並使用 IntelliSense 來瀏覽行動裝置平台的原生 API。For example, you'll use a designer to create your pages, and use IntelliSense to explore the native API's of the mobile platforms. 當您準備好要執行應用程式及查看其外觀時,您可以使用 Android SDK 模擬器,並以原生方式執行 Windows 應用程式。When you're ready to run your app and see how it looks, you can use the Android SDK emulator and run Windows apps natively. 您也可以直接使用已連接到開發電腦的 Android 和 Windows 裝置。You can also use tethered Android and Windows devices directly. 針對 iOS 專案,請連線到網路上的 Mac,然後從 Visual Studio 啟動 iOS 模擬器,或者連線到已連接到開發電腦的裝置。For iOS projects, connect to a networked Mac and start the iOS emulator from Visual Studio, or connect to a tethered device.

設計一組頁面,使用 Xamarin.Forms 在所有裝置上轉譯Design one set of pages that render across all devices by using Xamarin.Forms

根據您應用程式設計的複雜度,您可能會考慮使用專案範本之 [行動應用程式] 群組中的 Xamarin.Forms 範本,來建置應用程式。Depending on the complexity of your apps design, you might consider building it by using Xamarin.Forms templates in the Mobile Apps group of project templates. Xamarin.Forms 是可讓您建立單一使用者介面的 UI 工具組,您可以跨 Android、iOS 和 Windows 共用該介面。Xamarin.Forms is a UI toolkit that lets you create a single interface that you can share across Android, iOS, and Windows. 當您編譯 Xamarin.Forms 方案時,將取得 Android 應用程式、iOS 應用程式和 Windows 應用程式。When you compile a Xamarin.Forms solution, you'll get an Android app, an iOS app, and a Windows app. 如需詳細資訊,請參閱了解如何使用 Xamarin 進行行動裝置應用程式開發Xamarin.Forms 文件For more details, see Learn about mobile development with Xamarin and the Xamarin.Forms documentation.

在 Android、iOS 和 Windows 應用程式之間共用程式碼Share code between Android, iOS, and Windows apps

如果您不使用 Xamarin.Forms,並選擇針對每個平台個別設計,您可以在平台專案 (Android、iOS 和 Windows) 之間共用大部分的非 UI 程式碼。If you're not using Xamarin.Forms and choose to design for each platform individually, you can share most of your non-UI code between platform projects (Android, iOS, and Windows). 這包括任何商務邏輯、雲端整合、資料庫存取,或以 .NET Framework 為目標的其他任何程式碼。This includes any business logic, cloud integration, database access, or any other code that targets the .NET Framework. 唯一無法共用的程式碼是以特定平台為目標的程式碼。The only code that you can't share is code that targets a specific platform.

在 Windows、iOs 和 Android UI 之間共用程式碼Share code between Windows, iOs, and Android UI's

您可以使用共用專案和 (或) 可攜式類別庫專案來共用您的程式碼。You can share your code by using a shared project, a Portable Class Library project, or both. 您可能會發現某些程式碼最適合用於共用專案,而某些程式碼在可攜式類別庫專案內較有意義。You might find that some code fits best in a shared project, and some code makes more sense inside a Portable Class Library project.

進一步了解Learn more
共用程式碼選項 (英文) (Xamarin)Sharing Code Options (Xamarin)
.NET 的程式碼共用選項Code sharing options with .NET

以 Windows 10 裝置為目標Target Windows 10 devices

Windows 裝置Windows Devices

如果您想要建立以 Windows 10 裝置的完整範圍為目標的單一應用程式,請建立通用 Windows 應用程式。If you want to create a single app that targets the full breadth of Windows 10 devices, create a universal Windows app. 您會使用單一專案來設計應用程式,您的網頁將正確轉譯,無論是使用哪一種裝置來檢視。You'll design the app by using a single project and your pages will render properly no matter what device is used to view them.

請從通用 Windows 平台 (UWP) 應用程式專案範本開始。Start with a Universal Windows Platform (UWP) app project template. 以視覺化的方式,設計您的網頁,然後在預覽視窗中開啟它們,以查看它們在各種裝置上的外觀。Design your pages visually, and then open them in a preview window to see how they appear for various types of devices. 如果您不喜歡網頁在裝置上的外觀,您可以最佳化成更適合螢幕大小、解析度或不同的方向,例如橫向或縱向模式。If you don't like how a page appears on a device, you can optimize the page to better fit the screen size, resolution, or various orientations such as landscape or portrait mode. 您可以使用直覺式的工具視窗和 Visual Studio 中容易存取的功能表選項來完成這些操作。You can do all of that by using intuitive tool windows and easily accessible menu options in Visual Studio. 當您準備好要執行應用程式並逐步執行程式碼時,會發現所有裝置模擬器和不同類型的裝置的模擬器一起位在 [標準] 工具列上的一個下拉式清單。When you're ready to run your app and step through your code, you'll find all of the device emulators and simulators for different types of devices together in one drop-down list that is located on the Standard toolbar.

進一步了解Learn more
通用 Windows 平台簡介Intro to the Universal Windows Platform
建立您的第一個應用程式Create your first app
開發適用於通用 Windows 平台 (UWP) 的應用程式Develop apps for the Universal Windows Platform (UWP)
將應用程式移轉至通用 Windows 平台 (UWP)Migrate apps to the Universal Windows Platform (UWP)

建置 Android、iOS 和 Windows 的應用程式 (HTML/JavaScript)Build an app for Android, iOS, and Windows (HTML/JavaScript)

Windows、iOS 和 Android 裝置Windows, iOS, and Android devices

如果您是熟悉 HTML 和 JavaScript 的 Web 開發人員,則可使用 Visual Studio Tools for Apache Cordova,以 Windows、Android 和 iOS 為目標。If you're a web developer, and you're familiar with HTML and JavaScript, you can target Windows, Android, and iOS by using Visual Studio Tools for Apache Cordova. 這些應用程式可以全部三個平台為目標,且您可以使用最熟悉的技巧和程序來建置它們。These apps can target all three platforms and you can build them by using the skills and processes that you're most familiar with.

Apache Cordova 是一種包含外掛程式模型的架構。Apache Cordova is a framework that includes a plug-in model. 此外掛程式模型提供單一 JavaScript API,讓您可用來存取這三個平台 (Android、iOS 和 Windows) 的原生裝置功能。This plug-in model provides a single JavaScript API that you can use to access the native device capabilities of all three platforms (Android, iOS, and Windows).

因為這些 API 是跨平台的,您可以在所有三個平台之間共用您撰寫的大部分內容。Because these APIs are cross-platform, you can share most of what you write between all three platforms. 這會降低開發與維護的成本。This reduces your development and maintenance costs. 另外,也不需要從頭開始。Also, there's no need to start from scratch. 如果您已建立其他類型的 Web 應用程式,可以與 Cordova 應用程式共用那些檔案,而不需要以任何方式修改或重新設計。If you've created other types of web applications, you can share those files with your Cordova app without having to modify or redesign them in any way.

使用 JAVAscript 的多重裝置混合式應用程式Multi-device hybrid apps with Javascript

首先,安裝 Visual Studio,然後在設定期間選擇 [使用 JavaScript 進行行動開發] 功能。To get started, install Visual Studio and choose the Mobile Development with Javascript feature during setup. Cordova 工具會自動安裝建置多重平台應用程式所需的所有協力廠商軟體。The Cordova tools automatically install all third-party software that's required to build your multi-platform app.

安裝延伸模組之後,請開啟 Visual Studio 並建立空白應用程式 (Apache Cordova) 專案。After you've installed the extension, open Visual Studio and create a Blank App (Apache Cordova) project. 然後,您可以使用 JavaScript 或 TypeScript 來開發應用程式。Then, you can develop your app by using JavaScript or Typescript. 您也可以加入外掛程式以擴充應用程式的功能,而外掛程式中的 API 會在您撰寫程式碼時出現在 IntelliSense 中。You can also add plug-ins to extend the functionality of your app, and APIs from plug-ins appear in IntelliSense as you write code.

當您準備好要執行應用程式並逐步執行程式碼時,請選擇模擬器,例如 Apache Ripple 模擬器或 Android 模擬器、瀏覽器或您已直接連接到電腦的裝置。When you're ready to run your app and step through your code, choose an emulator, such as the Apache Ripple emulator or Android Emulator, a browser, or a device that you've connected directly to your computer. 然後啟動您的應用程式。Then, start your app. 如果您正在 Windows PC 上開發應用程式,您甚至可以在其上執行。If you're developing your app on a Windows PC, you can even run it on that. 系統會將這些選項全都建置到 Visual Studio 中,以做為 Visual Studio Tools for Apache Cordova 的一部分。All of these options are built into Visual Studio as part of the Visual Studio Tools for Apache Cordova.

Visual Studio 仍然提供用於建立通用 Windows 平台 (UWP) 應用程式的專案範本;因此,請只在想要以 Windows 裝置為目標時使用它們。Project templates for creating Universal Windows Platform (UWP) apps are still available in Visual Studio so feel free to use them if you plan to target only Windows devices. 如果您之後決定以 Android 和 iOS 為目標,一律可將程式碼移植到 Cordova 專案。If you decide to target Android and iOS later, you can always port your code to a Cordova project.

進一步了解Learn more
安裝 Visual Studio (VisualStudio.com)Install Visual Studio (VisualStudio.com)
開始使用 Visual Studio Tools for Apache CordovaGet started with Visual Studio Tools for Apache Cordova
了解 Visual Studio 的 Android 模擬器 (VisualStudio.com)Learn about the Visual Studio Emulator for Android (VisualStudio.com)

建立適用于 Android、iOS 和 Windows 的應用程式C++()Build an app for Android, iOS, and Windows (C++)

使用 C+ +針對 Android、iOS 和 Windows 建立Use C++ to build for Android, iOS, and Windows

首先,安裝 Visual Studio 和使用工作負載進行C++ 行動裝置開發。First, install Visual Studio and the Mobile Development with C++ workload. 接著,您可以建立 Android 的原生活動應用程式,或以 Windows 或 iOS 為目標的應用程式。Then, you can build a native activity application for Android, or an app that targets Windows or iOS. 如有需要,您可以在相同的方案中以 Android、iOS 和 Windows 為目標,然後使用跨平臺靜態或動態共用程式庫,在兩者之間共用程式碼。You can target Android, iOS, and Windows in the same solution if you want, and then share code between them by using a cross-platform static or dynamic shared library.

如果您必須建置需要任何進階圖形操作類型 (例如遊戲) 的 Android 應用程式,可使用 C++ 來執行此動作。If you need to build an app for Android that requires any sort of advanced graphics manipulation, such as a game, you can use C++ to do it. 原生活動應用程式(Android) 專案開始。Start with the Native Activity Application (Android) project. 這個專案提供對 Clang 工具鏈的完整支援。This project has full support for the Clang toolchain.

Native activity 專案範本Native activity project template

當您準備好要執行應用程式及查看其外觀時,請使用適用於 Android 模擬器。When you're ready to run your app and see how it looks, use the Android Emulator. 這項工具不僅快速可靠,而且容易安裝及設定。It's fast, reliable, and easy to install and configure.

您也可以使用 C++ 和通用 Windows 平台 (UWP) 應用程式專案範本,建置以 Windows 10 裝置的完整範圍為目標的應用程式。You can also build an app that targets the full breadth of Windows 10 devices by using C++ and a Universal Windows Platform (UWP) app project template. 在本主題稍早的以 Windows 10 裝置為目標一節中進一步了解。Read more about this in the Target Windows 10 devices section that appears earlier in this topic.

您可以藉C++由建立靜態或動態共用程式庫,在 Android、IOS 和 Windows 之間共用程式碼。You can share C++ code between Android, iOS, and Windows by creating a static or dynamic shared library.

靜態和動態共用程式庫Static and dynamic shared libraries

您可以在 Windows、iOS 或 Android 專案中使用該程式庫,如本節稍早所述。You can consume that library in a Windows, iOS, or Android project, like the ones described earlier in this section. 您也可以在使用 Xamarin、Java 或任何語言建置的應用程式中使用該程式庫,讓您叫用 Unmanaged DLL 中的函式。You can also consume it in an app that you build by using Xamarin, Java, or any language that lets you invoke functions in an unmanaged DLL.

當您在這些程式庫中撰寫程式碼時,您可以使用 IntelliSense 來瀏覽 Android 和 Windows 平台的原生 API。As you write code in these libraries, you can use IntelliSense to explore the native APIs of the Android and Windows platforms. 這些程式庫專案與 Visual Studio 偵錯工具完全整合,因此您可以使用偵錯工具的所有進階功能,來設定中斷點、逐步執行程式碼,以及尋找和修正問題。These library projects are fully integrated with the Visual Studio debugger so you can set breakpoints, step through code, and find and fix issues by using all of the advanced features of the debugger.

進一步了解Learn more
下載 Visual Studio (VisualStudio.com)Download Visual Studio (VisualStudio.com)
使用安裝跨平臺行動開發C++Install cross-platform mobile development with C++
深入瞭解如何使用C++將目標設為多個平臺(VisualStudio.com)Learn more about using C++ to target multiple platforms (VisualStudio.com)
安裝您需要的內容,然後建立適用于 Android 的原生活動應用程式Install what you need, and then create a native activity application for Android
深入了解如何與 Android 和 Windows 應用程式共用 C++ 程式碼 (VisualStudio.com)Learn more about sharing C++ code with Android and Windows apps (VisualStudio.com)
適用于的跨平臺行動裝置開發範例C++Cross-platform mobile development examples for C++

使用 Visual Studio Tools for Unity 建置 Android、iOS 和 Windows 的跨平台遊戲Build a cross-platform game for Android, iOS, and Windows by using Visual Studio tools for Unity

Visual Studio Tools for Unity 是將 Visual Studio 的強大程式碼編輯、生產力和偵錯工具與 Unity 整合的免費 Visual Studio 延伸模組,這個熱門的跨平台遊戲/圖形引擎和開發環境適用於以 Windows、iOS、Android 和其他平台為目標的沉浸式應用程式。Visual Studio Tools for Unity is a free extension for Visual Studio that integrates Visual Studio's powerful code editing, productivity, and debugging tools with Unity, the popular cross-platform gaming/graphics engine and development environment for immersive apps that target Windows, iOS, Android, and other platforms including the web.

VSTU 開發環境VSTU development environment

透過 Visual Studio Tools for Unity (VSTU),您可以在 C# 中使用 Visual Studio 來撰寫遊戲和編輯器指令碼,然後使用其強大的偵錯工具來尋找及修正錯誤。With Visual Studio Tools for Unity (VSTU), you can use Visual Studio to write game and editor scripts in C# and then use its powerful debugger to find and fix errors. 最新版的 VSTU 包含對 Unity 2018.1 的支援,且包括 Unity 的 ShaderLab 著色器語言的語法著色、與 Unity 的更佳同步化、更豐富的偵錯,以及 MonoBehavior 精靈的改善式程式碼產生。The latest release of VSTU brings support for Unity 2018.1 and includes syntax coloring for Unity's ShaderLab shader language, better synchronization with Unity, richer debugging, and improved code generation for the MonoBehavior wizard. VSTU 也會將您的 Unity 專案檔案、主控台訊息和啟動遊戲的功能整合到 Visual Studio 中,以便您可以在撰寫程式碼時,花較少的時間來切換 Unity Editor。VSTU also brings your Unity project files, console messages, and the ability to start your game into Visual Studio so you can spend less time switching to and from the Unity Editor while writing code.

進一步了解Learn more
進一步了解如何使用 Visual Studio 來建置 Unity 遊戲Learn more about building Unity games with Visual Studio
深入了解 Visual Studio Tools for UnityRead more about Visual Studio Tools for Unity
開始使用 Visual Studio Tools for UnityStart using Visual Studio Tools for Unity
了解 Visual Studio Tools for Unity 2.0 Preview 的最新增強功能 (Visual Studio 部落格)Read about the latest enhancements to the Visual Studio Tools for Unity 2.0 Preview (Visual Studio blog)
觀賞 Visual Studio Tools for Unity 2.0 Preview 的介紹影片 (影片)Watch a video introduction to the Visual Studio Tools for Unity 2.0 Preview (Video)
了解 Unity (Unity 網站)Learn about Unity (Unity website)

請參閱See also