從 Windows Phone Silverlight 移到 UWPMove from Windows Phone Silverlight to UWP

如果您是 Windows Phone Silverlight app 的開發人員,便可以在移到 Windows 10 時,充分利用您的技能組合與原始程式碼。If you’re a developer with a Windows Phone Silverlight app, then you can make great use of your skill set and your source code in the move 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 app,以及調適型程式碼與調適型 UI 概念的詳細背景資訊,請參閱通用 Windows 平台 (UWP) app 指南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 the Guide to Universal Windows Platform (UWP) apps.

當您將 Windows Phone Silverlight 應用程式移植成 Windows 10 應用程式時,將可補足已在 Windows Phone 8.1 引入的行動裝置功能,還能超越它們,使用可在所有 Windows 10 裝置通用的應用程式模型與 UI 架構之「通用 Windows 平台 (UWP)」。When you port your Windows Phone Silverlight app to a Windows 10 app, you'll be able to catch up on the mobile features that were introduced in Windows Phone 8.1, and go far beyond them to use the Universal Windows Platform (UWP) whose app model and UI framework are universal across all Windows 10 devices. 這使得以一個程式碼為基底,以及單一應用程式套件,來支援電腦、平板電腦、手機與大量其他種類裝置成為可能。That makes it possible to support PCs, tablets, phones, and a large number of other kinds of devices, from one code base and with one app package. 而這將讓您 app 的潛在對象倍增,並藉由共用資料、購買的消費性產品等,創造新的可能性。And that will multiply your app's potential audience and create new possibilities with shared data, purchased consumables, and so on. 如需新功能的詳細資訊,請參閱 Windows 10 提供給開發人員的新功能For more info on new features, see What's new for developers in Windows 10.

您也可以選擇將 app 的 Windows Phone Silverlight 版本與 Windows 10 版本同時提供給客戶。If you choose to, the Windows Phone Silverlight version of your app and the Windows 10 version of it can both be available to customers at the same time.

注意   本指南的設計目的是協助您將 Windows Phone Silverlight 應用程式移植到手動 Windows 10。Note  This guide is designed to help you port your Windows Phone Silverlight app to Windows 10 manually. 除了使用本指南中的資訊移植您的 app 外,您還可嘗試 Mobilize.NET 的 Silverlight Bridge 的開發人員預覽以協助移植程序自動化。In addition to using the information in this guide to port your app, you can try the developer preview of Mobilize.NET's Silverlight Bridge to help automate the porting process. 此工具會分析您的應用程式的原始程式碼,並將 Windows Phone Silverlight 控制項和 API 的參考轉換成其 UWP 對應項目。This tool analyzes your app's source code and converts references to Windows Phone Silverlight controls and APIs to their UWP counterparts. 因為此工具仍在開發人員預覽中,尚無法處理所有轉換案例。Because this tool is still in developer preview, it does not yet handle all conversion scenarios. 不過,大部分的開發人員應可藉由開始使用此工具而節省一些時間和精力。However, most developers should be able to save some time and effort by starting with this tool. 若要嘗試開發人員預覽,請瀏覽 Mobilize.NET 的網站To try the developer preview, visit Mobilize.NET's website.

XAML 與 .NET 或 HTML?XAML and .NET, or HTML?

Windows Phone Silverlight 有以 Silverlight 4.0 為基礎的 XAML UI 架構,您可以針對 .NET Framework 的版本和 Windows 執行階段 Api 的一小部分進行程式設計。Windows Phone Silverlight has a XAML UI framework based on Silverlight 4.0, and you program against a version of the .NET Framework and a small subset of Windows Runtime APIs. 由於您已在 Windows Phone Silverlight 應用程式中使用 Extensible Application Markup Language (XAML),XAML 應該是適合用在您 Windows 10 版本的選擇,因為您多數的知識與經驗,以及大部分的原始程式碼與所使用的軟體模式都會一併轉移過去。Since you used Extensible Application Markup Language (XAML) in your Windows Phone Silverlight app, it's likely that XAML will be your choice for your Windows 10 version because most of your knowledge and experience will transfer, as will much of your source code and the software patterns you use. 甚至您的 UI 標記與設計也可以輕易地移植過去。Even your UI markup and design can port over readily. 您將發現 Managed API、XAML 標記、UI 架構與工具全都令人熟悉不已,而且您可以在 UWP app 中使用 C++、C# 或 Visual Basic 來搭配 XAML。You will find the managed APIs, the XAML markup, the UI framework, and the tooling all reassuringly familiar, and you can use C++, C#, or Visual Basic along with XAML in a UWP app. 您可能會訝異此程序相對上有多麼容易,即使過程中有一兩項挑戰。You may be surprised at how relatively easy the process is, even if there is a challenge or two along the way.

請參閱使用 C# 或 Visual Basic 建立通用 Windows 平台 (UWP) app 的藍圖See Roadmap for Universal Windows Platform (UWP) apps using C# or Visual Basic.

注意   Windows 10 支援比 Windows Phone Store 應用程式更多的 .NET Framework。Note  Windows 10 supports much more of the .NET Framework than a Windows Phone Store app does. 例如,Windows 10 有數個 System.servicemodel。 * 命名空間以及 System.Net、System System.net.networkinformation.networkinformationexception 和 System .Net。For example, Windows 10 has several System.ServiceModel.* namespaces as well as System.Net, System.Net.NetworkInformation, and System.Net.Sockets. 因此,現在是移植您的 Windows Phone Silverlight,讓您的 .NET 程式碼直接編譯並在新平台上運作的絕佳時機。So, now is a great time to port your Windows Phone Silverlight and have your .NET code just compile and work on the new platform. 請參閱命名空間與類別對應See Namespace and class mappings. 將現有 .NET 原始程式碼重新編譯為 Windows 10 應用程式另一個極好的理由是,您將可以從 .NET 原生 (一種事先編譯技術,其將 MSIL 轉換成原生可執行機器碼) 獲得助益。Another great reason to recompile your existing .NET source code into a Windows 10 app is that you will benefit from .NET Native, which an ahead-of-time compilation technology that converts MSIL into natively-runnable machine code. .NET 原生 app 比其對應的 MSIL 啟動更快、使用更少的記憶體,而且消耗較少的電池電力。.NET Native apps start faster, use less memory, and use less battery than their MSIL counterparts.

此移植指南將著重于 XAML,但您也可以建立功能相當的應用程式,使用 JavaScript、階層式樣式表 (CSS) 和 HTML5 以及適用於 JavaScript 的 Windows Library 來呼叫許多相同的 Windows 執行階段 Api。This porting guide will focus on XAML but, alternatively, you can build a functionally equivalent app—calling many of the same Windows Runtime APIs—using JavaScript, Cascading Style Sheets (CSS), and HTML5 along with the Windows Library for JavaScript. 雖然 XAML 與 HTML 的 Windows 執行階段 UI 架構彼此並不相同,但是不論您選擇哪一個,都可在各種 Windows 裝置上通用。Although the Windows Runtime UI frameworks of XAML and HTML are different from one another, whichever one you choose will work universally across the full range of Windows devices.

以通用或行動裝置系列為目標Targeting the universal or the mobile device family

有一個選項是將您的應用程式移植成以通用裝置系列為目標的應用程式。One option you have is to port your app to an app that targets the universal device family. 在此情況下,應用程式可以安裝到最多種類的裝置上。In this case, the app can be installed onto the widest range of devices. 如果應用程式呼叫只在行動裝置系列中實作的 API,您就可以使用調適型程式碼來保護那些呼叫。If your app calls APIs that are implemented only in the mobile device family, then you can guard those calls with adaptive code. 或者,您可以選擇將應用程式移植成以行動裝置系列為目標的應用程式,這種情況您不需撰寫調適型程式碼。Alternatively, you can choose to port your app to an app that targets the mobile device family in which case you don't need to write adaptive code.

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

您在前面小節中所做的選擇將決定可用來執行應用程式的裝置範圍,而且此裝置範圍可能非常廣泛。The option you choose from the previous section 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, since your app will be running on form factors that it didn't formerly support, test your UI on those form factors and make any change necessary so that your UI adapts appropriately on each. 您可以把這個當成移植後工作或移植延展目標,而在 Bookstore2 案例研究中會有一些實際操作範例。You can think of this is a post-porting task, or a porting stretch-goal, and there is an example of it in practice in the Bookstore2 case study.

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

  • 查看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, much of 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 API (包括用於資料存取的 API)。Both view model and data model code also use non-visual operating system and .NET APIs (including APIs for data-access). 其中大多數皆可供 UWP app 使用,因此您應該能夠原封不動地移植此程式碼中的大部分。And the vast majority of those are available to a UWP app, so you can expect to be able to port much of this code without change. 不過請記住:視圖模型是視圖的模型或 抽象概念Remember, though: a view model is a model, or abstraction, of a view. 檢視模型提供 UI 的狀態與行為,而檢視本身則提供視覺效果。A view model provides the state and behavior of UI, while the view itself provides the visuals. 基於這個原因,您為了配合 UWP 允許您在其上執行的不同尺寸而調整的任何 UI,可能都需要有對應的檢視模型變更。For this reason, any UI you adapt to the different form factors that the UWP allows you to run on will likely need corresponding view model changes. 針對網路功能和呼叫雲端服務,您通常可以選擇使用 .NET 或 Windows 執行階段 Api。For networking and calling cloud services, you typically have the option between using .NET or Windows Runtime APIs. 如需涉及做出決定的因素,請參閱雲端服務、網路功能及資料庫For the factors involved in making that decision, see Cloud services, networking, and databases.
  • 雲端服務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 app 的一個絕佳雲端服務選項,它提供強大的後端元件,可供通用 Windows 應用程式呼叫以取得服務,服務涵蓋範圍可從簡單的動態磚更新,到伺服器陣列可提供的那種高難度延展性。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 Universal Windows apps 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. 將 UWP app 分解為如上所述的分層,可讓您更容易更正 app、加以測試,並進行後續的閱讀與維護。Factoring your UWP 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. 您可以讓功能更容易重複使用及避免平台之間的一些 UI API 差異問題,只要依照 Model-View-ViewModel (MVVM) 模式即可。You can make functionality more reusable—and avoid some issues of UI API differences between platforms—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.

此規則的幾個例外One or two exceptions to the rule

當您閱讀此移植指南時,您可以參考命名空間與類別對應As you read this porting guide, you can refer to Namespace and class mappings. 相當直接的對應是一般規則,而且命名空間與類別對應表格描述任何的例外。Fairly straightforward mapping is the general rule, and the namespace and class mappings table describes any exceptions.

在功能層級,好消息是 UWP 中很少有不支援的項目。At the feature level, the good news is that there's very little that's not supported in the UWP. 您的技能組合與原始程式碼大部分都能非常好地轉譯到 UWP app,您將在此移植指南的剩餘部分閱讀到相關資訊。Most of your skill set and source code translates very well over to UWP apps, as you'll read in the rest of this porting guide. 但是,以下是幾個您可能已使用但找不到其 UWP 對等功能的 Windows Phone Silverlight 功能。But, here are the few Windows Phone Silverlight features that you may have used for which there is no UWP equivalent.

沒有 UWP 對等功能的功能Feature for which there is no UWP equivalent Windows Phone Silverlight 功能的文件Windows Phone Silverlight documentation for the feature
Microsoft XNA。Microsoft XNA. 一般而言,使用 C++ 的 Microsoft DirectX 可以做為替代。In general, Microsoft DirectX using C++ is the replacement. 請參閱開發遊戲DirectX 與 XAML 互通性See Developing games and DirectX and XAML interop. XNA Framework 類別庫XNA Framework Class Library
鏡頭應用程式Lens apps Windows Phone 8 的鏡頭Lenses for Windows Phone 8

 

主題Topic 說明Description
命名空間與類別對應Namespace and class mappings 本主題提供 Windows Phone Silverlight API 與其 UWP 對等 API 的完整對應。This topic provides a comprehensive mapping of Windows Phone Silverlight APIs to their UWP equivalents.
移植專案Porting the project 您將在 Visual Studio 中建立新的 Windows 10 專案,然後將您的檔案複製到此專案中,以開始移植程序。You begin the porting process by creating a new Windows 10 project in Visual Studio and copying 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 的做法可以極順利地從 Windows Phone Silverlight 轉譯至 UWP app。The practice of defining UI in the form of declarative XAML markup translates extremely well from Windows Phone Silverlight to UWP apps. 您會發現在更新系統資源索引鍵參考、變更某些元素類型名稱,以及將 "clr-namespace" 變更為 "using" 之後,大部分的標記都相容。You'll find that large sections of your markup are compatible once you've updated system Resource key references, changed some element type names, and changed "clr-namespace" to "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.
移植商務與資料層Porting business and data layers UI 的背後是商務與資料層。Behind your UI are your business and data layers. 這些層中的程式碼會呼叫作業系統和 .NET Framework API (例如背景處理、位置、相機、檔案系統、網路及其他資料存取)。The code in these layers calls operating system and .NET Framework APIs (for example, background processing, location, the camera, the file system, network, and other data access). 其中大多數皆可供 UWP app 使用,因此您應該能夠原封不動地移植此程式碼中的大部分。The vast majority of those are available to a UWP app, so you can expect to be able to port much of this code without change.
尺寸與 UX 的移植Porting for form factor and UX Windows app 在電腦、行動裝置與任何其他類型的裝置,都有相同的外觀及操作方式。Windows apps share a common look-and-feel across PCs, mobile devices, and many other kinds of devices. 使用者介面、輸入及互動模式皆非常相似,而在裝置之間移動的使用者會欣然感受到熟悉的體驗。The user interface, input, and interaction patterns are very similar, and a user moving between devices will welcome the familiar experience.
案例研究:Bookstore1Case study: Bookstore1 本主題提供將一個非常簡單的 Windows Phone Silverlight app 移植到 Windows 10 UWP app 的案例研究。This topic presents a case study of porting a very simple Windows Phone Silverlight app to a Windows 10 UWP app. 您可以使用 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.
案例研究:Bookstore2Case study: Bookstore2 這個案例研究 (根據 Bookstore1 中所提供的資訊來建置) 是從在 LongListSelector 中顯示分組資料的 Windows Phone Silverlight App 開始著手。This case study—which builds on the info given in Bookstore1—begins with a Windows Phone Silverlight app that displays grouped data in a LongListSelector. 在檢視模型中,每個 Author 類別執行個體都代表該作者所著之書籍的群組,而在 LongListSelector 中,我們可以檢視依作者分組的書籍清單,或是縮小來查看作者的捷徑清單。In the view model, each instance of the class Author represents the group of the books written by that author, and in the LongListSelector, we can either view the list of books grouped by author or we can zoom out to see a jump list of authors.

文件集Documentation

雜誌文章Magazine articles

簡報Presentations