從 Windows Runtime 8.x 移至 UWPMove from Windows Runtime 8.x to UWP

如果您有通用 8.1 app (無論它是針對 Windows 8.1、Windows Phone 8.1 或這兩者設計),則會發現您的原始程式碼和技能將可順暢地移植到 Windows 10。If you have a Universal 8.1 app—whether it's targeting Windows 8.1, Windows Phone 8.1, or both—then you'll find that your source code and skills will port smoothly to Windows 10. 您可以使用 Windows 10 來建立通用 Windows 平台 (UWP) app,這是可供客戶安裝至各種類型裝置的單一應用程式套件。With Windows 10, you can create a Universal Windows Platform (UWP) app, which is a single app package that your customers can install onto every kind of device. 如需我們將在本移植指南中提及之 Windows 10、UWP 應用程式,以及調適型程式碼與調適型 UI 概念的詳細背景資訊,請參閱 UWP 應用程式指南For more background on Windows 10, UWP apps, and the concepts of adaptive code and adaptive UI that we'll mention in this porting guide, see Guide to UWP apps.

在移植時,您將發現 Windows 10 會與先前的平台共用大部分的 API,以及 XAML 標記、UI 架構及工具,而且全都讓人非常熟悉。While porting, you'll find that Windows 10 shares the majority of APIs with the previous platforms, as well as XAML markup, UI framework, and tooling, and you'll find it all reassuringly familiar. 就像以前一樣,您還是可以選擇 C++、C# 或 Visual Basic 做為與 XAML UI 架構搭配使用的程式設計語言。Just as before, you can still choose between C++, C#, and Visual Basic for the programming language to use along with the XAML UI framework. 當您計劃目前的應用程式將執行哪些工作時,第一個步驟將視您的應用程式類型與專案類型而定。Your first steps in planning exactly what to do with your current app or apps will depend on the kinds of apps and projects you have. 下列小節中會加以說明。That's explained in the following sections.

如果您有通用 8.1 應用程式If you have a Universal 8.1 app

通用 8.1 app是從 8.1 通用 app 專案建置的。A Universal 8.1 app is built from an 8.1 Universal App project. 假設專案的名稱是 AppName _ 81。Let's say the project's name is AppName_81. 它包含這些子專案。It contains these sub-projects.

  • AppName _ 81. Windows。AppName_81.Windows. 這是建置適用於 Windows 8.1 之應用程式套件的專案。This is the project that builds the app package for Windows 8.1.
  • AppName _ 81. WindowsPhone。AppName_81.WindowsPhone. 這是建立適用於 Windows Phone 8.1 之應用程式套件的專案。This is the project that builds the app package for Windows Phone 8.1.
  • AppName _ 81. Shared。AppName_81.Shared. 這個專案當中包含其他兩個專案都會用到的原始程式碼、標記檔案及其他資產與資源。This is the project that contains source code, markup files, and other assets and resources that are used by both of the other two projects.

通常,8.1 通用 Windows app 會以Windows 8.1 與 Windows Phone 8.1 兩種形式—使用相同的程式碼和標記—來提供相同的功能。Often, an 8.1 Universal Windows app offers the same features—and does so using the same code and markup—in both its Windows 8.1 and Windows Phone 8.1 forms. 這類 app 最適合移植到針對通用裝置系列設計的單一 Windows 10 應用程式 (這表示它所能安裝的裝置種類最廣泛)。An app like that is an ideal candidate for porting to a single Windows 10 app that targets the Universal device family (and that you can install onto the widest range of devices). 基本上,您將會移植共用專案的內容,還需要使用來自其他兩個專案的少數內容 (或完全不使用),因為其他兩個專案中只有少數內容適用 (或完全不適用) 。You'll essentially port the contents of the Shared project and you'll need to use little or nothing from the other two projects because there'll be little or nothing in them.

其他時候,Windows 8.1 和/或 Windows Phone 8.1 形式的應用程式會包含獨特的功能。Other times, the Windows 8.1 and/or the Windows Phone 8.1 form of the app contain unique features. 或者它們包含相同的功能,但它們使用不同的技巧或不同的技術來實作它們。Or they contain the same features but they implement those features using different techniques or different technology. 使用這樣的應用程式時,您可以選擇將它移植到針對通用裝置系列設計的單一應用程式 (在這種情況下,您會希望應用程式能針對不同的裝置自我調整),或者您可以選擇將它移植到多個應用程式 (例如一個應用程式針對傳統型裝置系列,另一個針對行動裝置系列)。With an app like that, you can choose to port it to a single app that targets the Universal device family (in which case you will want the app to adapt itself to different devices), or you can choose to port it as more than one app, perhaps one targeting the Desktop device family and another targeting the Mobile device family. 通用 8.1 應用程式的特性將決定哪一項選擇最適合您的案例。The nature of the Universal 8.1 app will determine which of these options is best for your case.

  1. 將共用專案的內容移植到針對通用裝置系列設計的應用程式。Port the contents of the Shared project to an app targeting the Universal device family. 如果可以的話,請回收 Windows 和 WindowsPhone 專案的任何其他內容,以便在應用程式中無條件地使用那些內容,或在您的應用程式當時執行所在的裝置上有條件地使用那些內容 (後者的行為稱為「調適型」** 行為)。If applicable, salvage any other content from the Windows and WindowsPhone projects, and use that content either unconditionally in the app or conditional on the device that your app happens to be running on at the time (the latter behavior is known as adaptive).
  2. 將 WindowsPhone 專案的內容移植到針對通用裝置系列設計的應用程式。Port the contents of the WindowsPhone project to an app targeting the Universal device family. 如果可以的話,請回收 Windows 專案中的任何其他內容,以便無條件或以調適型方式使用那些內容。If applicable, salvage any other content from the Windows project, using it either unconditionally or adaptively.
  3. 將 Windows 專案的內容移植到針對通用裝置系列設計的應用程式。Port the contents of the Windows project to an app targeting the Universal device family. 如果可以的話,請回收 WindowsPhone 專案中的任何其他內容,以便無條件或以調適型方式使用那些內容。If applicable, salvage any other content from the WindowsPhone project, using it either unconditionally or adaptively.
  4. 將 Windows 專案的內容移植到針對通用或傳統型裝置系列設計的應用程式,以及將 WindowsPhone 專案的內容移植到針對通用或行動裝置系列設計的應用程式。Port the contents of the Windows project to an app targeting the Universal or the Desktop device family and also port the contents of the WindowsPhone project to an app targeting the Universal or the Mobile device family. 您可以建立一個包含共用專案的解決方案,然後繼續在兩個專案之間共用原始程式碼、標記檔案及其他資產和資源。You can create a solution with a Shared project, and continue to share source code, markup files, and other assets and resources between the two projects. 或者您可以建立不同的解決方案,但仍然使用連結來共用相同的項目。Or, you can create different solutions and still share the same items using links.

如果您有 Windows 8.1 應用程式If you have a Windows 8.1 app

請將專案移植到針對通用或傳統型裝置系列設計的應用程式。Port the project to an app targeting the Universal or the Desktop device family. 如果您選擇通用裝置系列,且您的應用程式會呼叫只在傳統型裝置系列中實作的 API,您就可以使用調適型程式碼來保護那些呼叫。If you choose the Universal device family, and your app calls APIs that are implemented only in the Desktop device family, then you can guard those calls with adaptive code.

如果您有 Windows Phone 8.1 應用程式If you have a Windows Phone 8.1 app

請將專案移植到針對通用或行動裝置系列設計的應用程式。Port the project to an app targeting the Universal or the Mobile device family. 如果您選擇通用裝置系列,且您的應用程式會呼叫只在行動裝置系列中實作的 API,您就可以使用調適型程式碼來保護那些呼叫。If you choose the Universal device family, and your app calls APIs that are implemented only in the Mobile device family, then you can guard those calls with adaptive code.

調整您的應用程式以適應多種尺寸Adapting your app to multiple form factors

您在前面小節中所做的選擇將決定用來執行應用程式的裝置範圍,而且此裝置範圍可能非常廣泛。The option you choose from the previous sections will determine the range of devices that your app or apps will run on, and that may well be a very wide range of devices. 即使將您的應用程式限制只在行動裝置系列上執行,您還是必須支援非常多種不同的螢幕尺寸。Even limiting your app to the Mobile device family still leaves you with a wide range of screen sizes to support. 因此,如果您的應用程式將以之前未支援的尺寸執行,請用那些尺寸來測試 UI 並進行任何必要的變更,讓 UI 可針對各個尺寸適當地調適。So, if your app will be running on form factors that it didn't formerly support, then test your UI on those form factors and make any change necessary, so that your UI adapts appropriately on each. 您可以將這個當成後續移植工作Bookstore2QuizGame 案例研究中會有一些實際操作範例。You can think of this is a post-porting task, or a porting stretch-goal, and there are some examples of it in practice in the Bookstore2 and QuizGame case studies.

逐層進行移植Approaching porting layer-by-layer

將通用 8.1 應用程式移植到 UWP 應用程式的模型時,您所有的知識和經驗,以及大部分您所使用的原始程式碼、標記及軟體模式,都會一併轉移過去。When porting a Universal 8.1 app to the model for UWP apps, virtually all of your knowledge and experience will transfer, as will most of your source code and markup and the software patterns you use.

  • 查看View. 檢視 (連同檢視模型) 會構成您的應用程式 UI。The view (together with the view model) makes up your app's UI. 在理想的情況下,檢視是由繫結至檢視模型之可觀察屬性的標記所組成。Ideally, the view consists of markup bound to observable properties of a view model. 另一種模式 (常見且方便,但僅限短期) 是針對程式碼後置檔案中要直接操作 UI 元素的命令式程式碼。Another pattern (common and convenient, but only in the short term) is for imperative code in a code-behind file to directly manipulate UI elements. 不論是哪一種情況,您的 UI 標記和設計—甚至是操作 UI 元素的命令式程式碼—都會直接移植。In either case, your UI markup and design—and even imperative code that manipulates UI elements—will be straightforward to port.
  • 檢視模型和資料模型View models and data models. 即使您沒有正式遵守關注點分離模式 (例如 MVVM),您的應用程式中也無法避免會有執行檢視模型和資料模型的函式。Even if you don't formally embrace separation-of-concerns patterns (such as MVVM), there is inevitably code present in your app that performs the function of view model and data model. 檢視模型程式碼會利用 UI 架構命名空間中的類型。View model code makes use of types in the UI framework namespaces. 檢視模型和資料模型程式碼也都使用非視覺作業系統和 .NET Framework API (包括用於資料存取的 API)。Both view model and data model code also use non-visual operating system and .NET Framework APIs (including APIs for data-access). 而且那些 API 也都適用於 UWP 應用程式,所以此程式碼大部分 (如果不是全部的話) 都將在沒有任何改變的情況下移植。And those APIs are available for UWP apps, too, so most if not all of this code will port without change.
  • 雲端服務Cloud services. 您的應用程式某些部分 (也許佔相當大的部分) 可能是在雲端以服務的形式執行。It's likely that some of your app (perhaps a great deal of it) runs in the cloud in the form of services. 在用戶端裝置上執行的應用程式部分則會連線到那些服務。The part of the app running on the client device connects to those. 這是分散式 app 中最有可能在移植用戶端部分時保持不變的部分。This is the part of a distributed app most likely to remain unchanged when porting the client part. 如果您還沒有雲端服務,Microsoft Azure 行動服務會是您 UWP 應用程式的一個絕佳雲端服務選項,它提供強大的後端元件,可供您的應用程式呼叫以取得服務,服務涵蓋範圍可從簡單的動態磚更新,一直到伺服器陣列可提供的那種高難度延展性。If you don't already have one, a good cloud services option for your UWP app is Microsoft Azure Mobile Services, which provides powerful back-end components that your app can call for services ranging from simple notifications for live tiles updates up to the kind of heavy-lifting scalability a server farm can provide.

在移植之前或在移植期間,請考量是否可藉由重構來改善您的 app,以便將目的類似的程式碼一起收集在一些分層中,而不是任意散佈。Before or during the porting, consider whether your app could be improved by refactoring it so that code with a similar purpose is gathered together in layers and not scattered arbitrarily. 將應用程式分解為如上所述的分層,可讓您更容易更正應用程式、加以測試,並進行後續的讀取和維護。Factoring your app into layers like those described above makes it easier for you to make your app correct, to test it, and then subsequently to read and maintain it. 您可以遵循 Model-View-ViewModel (MVVM) 模式,讓功能具備更高的可重複使用性。You can make functionality more reusable by following the Model-View-ViewModel (MVVM) pattern. 這種模式會將您 app 的資料、商務及 UI 部分彼此分開。This pattern keeps the data, business, and UI parts of your app separate from one another. 即便是在 UI 中,它也能將狀態和行為分開,以及個別地將可測試項目從視覺效果分離。Even within the UI it can keep state and behavior separate, and separately testable, from the visuals. 使用 MVVM 時,您可以只撰寫一次您的資料與商務邏輯,然後在所有裝置上使用,而不需要擔心 UI 的問題。With MVVM, you can write your data and business logic once and use it on all devices no matter the UI. 您可能也可以在不同的裝置上重複使用大部分的檢視模型與檢視組件。It's likely that you'll be able to re-use much of the view model and view parts across devices, too.

主題Topic 說明Description
移植專案Porting the project 當您開始移植程序時,會有兩個選項。You have two options when you begin the porting process. 其中一個是編輯現有專案檔案的複本,包括應用程式套件資訊清單 (若要了解該選項,請參閱將 app 移轉至通用 Windows 平台 (UWP) 中關於更新您專案檔案的資訊)。One is to edit a copy of your existing project files, including the app package manifest (for that option, see the info about updating your project files in Migrate apps to the Universal Windows Platform (UWP)). 另一個選項是在 Visual Studio 中建立新的 Windows 10 專案,並將檔案複製到其中。The other option is to create a new Windows 10 project in Visual Studio and copy your files into it.
疑難排解Troubleshooting 強烈建議您將此移植指南從頭到尾讀一遍,但是我們也了解您急著想要儘快進入建置及執行專案的階段。We highly recommend reading to the end of this porting guide, but we also understand that you're eager to forge ahead and get to the stage where your project builds and runs. 為此,您可以暫時將任何非必要的程式碼標成註解或移除,稍後再回來清償該負債。To that end, you can make temporary progress by commenting or stubbing out any non-essential code, and then returning to pay off that debt later. 本主題中的疑難排解問題和解決方式表格雖然無法用來替代閱讀接下來的幾個主題,但是在這個階段可能對您很有幫助。The table of troubleshooting symptoms and remedies in this topic may be helpful to you at this stage, although it's not a substitute for reading the next few topics. 在您進展到後續的主題時,隨時可以回來參考這個表格。You can always refer back to the table as you progress through the later topics.
移植 XAML 與 UIPorting XAML and UI 以宣告式 XAML 標記形式定義 UI 的做法可以極順利地從通用 8.1 app 轉譯至 UWP app。The practice of defining UI in the form of declarative XAML markup translates extremely well from Universal 8.1 apps to UWP apps. 您會發現大部分標記都相容,雖然您可能需要對系統的「資源」索引鍵或您使用的自訂範本進行一些調整。You'll find that most of your markup is compatible, although you may need to make some adjustments to the system Resource keys or custom templates that you're using.
I/O、裝置與應用程式模型的移植Porting for I/O, device, and app model 與裝置本身及其感應器整合的程式碼牽涉到從使用者輸入和輸出到使用者。Code that integrates with the device itself and its sensors involves input from, and output to, the user. 它也可能涉及資料處理。It can also involve processing data. 但是這個程式碼通常不被認為是 UI 層或資料層。But, this code is not generally thought of as either the UI layer or the data layer. 這個程式碼包含與震動控制器、加速計、陀螺儀、麥克風和喇叭 (與語音辨識和合成交集)、(地理) 位置以及輸入形式 (例如觸控、滑鼠、鍵盤及手寫筆) 的整合。This code includes integration with the vibration controller, accelerometer, gyroscope, microphone and speaker (which intersect with speech recognition and synthesis), (geo)location, and input modalities such as touch, mouse, keyboard, and pen.
案例研究:Bookstore1Case study: Bookstore1 本主題提供的案例研究可將非常簡單的通用 8.1 app 移植到 Windows 10 UWP app。This topic presents a case study of porting a very simple Universal 8.1 app to a Windows 10 UWP app. 通用 8.1 app 會針對 Windows 8.1 建置一個應用程式套件,並針對 Windows Phone 8.1 建置另一個應用程式套件。A Universal 8.1 app is one that builds one app package for Windows 8.1, and a different app package for Windows Phone 8.1. 您可以使用 Windows 10,來建立可供客戶安裝至各種類型裝置的單一 app 套件,而那就是我們將在這個案例研究中執行的工作。With Windows 10, you can create a single app package that your customers can install onto a wide range of devices, and that's what we'll do in this case study. 請參閱 UWP app 指南See Guide to UWP apps.
案例研究:Bookstore2Case study: Bookstore2 這個案例研究是以 SemanticZoom 控制項中提供的資訊為基礎建置。This case study—which builds on the info given in SemanticZoom control. 在檢視模型中,每個 Author 類別執行個體都代表該作者所著之書籍的群組,而在 SemanticZoom 中,我們可以檢視依作者分組的書籍清單,或是縮小來查看作者的捷徑清單。In the view model, each instance of the class Author represents the group of the books written by that author, and in the SemanticZoom, we can either view the list of books grouped by author or we can zoom out to see a jump list of authors.
案例研究:QuizGameCase study: QuizGame 本主題提供的案例研究會將對等運作的測驗遊戲 WinRT 8.1 app 範例移植到 Windows 10 UWP app。This topic presents a case study of porting a functioning peer-to-peer quiz game WinRT 8.1 sample app to a Windows 10 UWP app.

文件集Documentation