從桌面應用程式移至 UWPMove from a desktop application to UWP

如果您現有的桌面應用程式是使用 .NET Framework (所建立,包括 WPF 和 Windows Forms) 或 c + + Win32 Api,您有數個選項可移至通用 Windows 平臺 (UWP) 和 Windows 10。If you have an existing desktop application that was built using the .NET Framework (including WPF and Windows Forms) or C++ Win32 APIs, you have several options for moving to the Universal Windows Platform (UWP) and Windows 10.

將您的桌面應用程式封裝在 MSIX 套件中Package your desktop application in an MSIX package

您可以在 MSIX 套件中封裝桌面應用程式,以存取更多 Windows 10 功能。You can package your desktop application in an MSIX package to get access to many more Windows 10 features. MSIX 是新式 Windows 應用程式套件格式,為所有 Windows 應用程式提供通用封裝體驗,包括 UWP、WPF、Windows Forms 及 Win32 應用程式。MSIX is a modern Windows app package format that provides a universal packaging experience for all Windows apps, including UWP, WPF, Windows Forms and Win32 apps. 將您的傳統型 Windows 應用程式封裝在 MSIX 套件中,可讓您存取強固的安裝和更新體驗、具有彈性功能系統的受控安全性模型、Microsoft Store 的支援、企業管理,以及許多自訂散發模型。Packaging your desktop Windows apps in MSIX packages gets you access to a robust installation and updating experience, a managed security model with a flexible capability system, support for the Microsoft Store, enterprise management, and many custom distribution models. 您可以封裝您的應用程式是否有原始程式碼,或者您是否只有現有的安裝程式檔案 (例如 MSI 或 App-v 安裝程式) 。You can package your application whether you have the source code or if you only have an existing installer file (such as an MSI or App-V installer). 封裝您的應用程式之後,您可以整合 UWP 功能,例如封裝延伸模組和其他 UWP 元件。After you package your application, you can integrate UWP features such as package extensions and other UWP components.

如需詳細資訊,請參閱 封裝桌面應用程式 (傳統型橋接器) 以及 需要套件身分識別的功能For more information, see Package desktop applications (Desktop Bridge) and Features that require package identity.

使用 Windows 執行階段 ApiUse Windows Runtime APIs

您可以在您的 WPF、Windows Forms 或 C++ Win32 傳統型應用程式中直接呼叫許多 Windows 執行階段 API,整合為 Windows 10 使用者帶來好處的新式體驗。You can call many Windows Runtime APIs directly in your WPF, Windows Forms, or C++ Win32 desktop app to integrate modern experiences that light up for Windows 10 users. 例如,您可以呼叫 Windows 執行階段 API 以將快顯通知新增至您的傳統型應用程式。For example, you can call Windows Runtime APIs to add toast notifications to your desktop app.

如需詳細資訊,請參閱在傳統型應用程式中使用 Windows 執行階段 APIFor more information, see Use Windows Runtime APIs in desktop apps.

將 .NET Framework 應用程式遷移至 UWP 應用程式Migrate a .NET Framework app to a UWP app

如果您的應用程式是在 .NET Framework 上執行,您可以利用 .NET Standard 2.0 將它遷移至 UWP 應用程式。If your application runs on the .NET Framework, you can migrate it to a UWP app by leveraging .NET Standard 2.0. 盡可能將多個程式碼移至 .NET Standard 2.0 類別庫,然後建立可參考 .NET Standard 2.0 程式庫的 UWP 應用程式。Move as much code as you can into .NET Standard 2.0 class libraries, and then create a UWP app that references your .NET Standard 2.0 libraries.

共用 .NET Standard 2.0 程式庫中的程式碼Share code in a .NET Standard 2.0 library

如果您的應用程式是在 .NET Framework 上執行,請盡可能將最多的程式碼放入 .NET Standard 2.0 類別庫中。If your application runs on the .NET Framework, place as much code as you can into .NET Standard 2.0 class libraries. 只要您的程式碼使用 Standard 中定義的 API,就可以在 UWP app 中重複使用該程式碼。As long as your code uses APIs that are defined in the standard, you can reuse it in a UWP app. 這比以往任何時候都還要容易共用 .NET Standard 程式庫的程式碼,因為 .NET Standard 2.0 中包含更加多的 API 了。It's easier than it's ever been to share code in a .NET Standard library because so many more APIs are included in the .NET Standard 2.0.

以下是告訴您詳細資訊的影片。Here's a video that tells you more about it.

新增 .NET Standard 程式庫Add .NET Standard libraries

首先將一個或多個 .NET Standard 類別庫新增至您的方案。First, add one or more .NET Standard class libraries to your solution.

新增 DotNet 標準專案

您新增至方案的程式庫數目取決於您想要如何組合管理程式碼。The number of libraries that you add to your solution depends on how you plan to organize your code.

請確定每個類別庫都是以 .NET Standard 2.0 為目標。Make sure that each class library targets the .NET Standard 2.0.

將目標設為 .NET Standard 2.0

您可以在類別庫專案的屬性頁中找到此設定。You can find this setting in the property pages of the class library project.

在傳統型應用程式專案中,新增類別庫專案的參考。From your desktop application project, add a reference to the class library project.

呼叫 .net 專案之類別庫參考的方案總管窗格螢幕擷取畫面。

接下來,使用工具來判斷您的程式碼有多少符合標準。Next, use tools to determine how much of your code conforms to the standard. 如此一來,將程式碼移入程式庫之前就可以決定您可重複使用哪些部分、哪些部分需要進行最基本的修改,以及哪些部分要保留給特定應用程式專用。That way, before you move code into the library, you can decide which parts you can reuse, which parts require minimal modification, and which parts will remain application-specific.

檢查程式庫與程式碼的相容性Check library and code compatibility

我們會從 Nuget 套件及其他從協力廠商取得的 dll 檔案開始著手。We'll start with Nuget Packages and other dll files that you obtained from a third party.

如果您的應用程式使用這其中任何一個,請判斷是否與 .NET Standard 2.0 相容。If your application uses any of them, determine if they are compatible with the .NET Standard 2.0. 若要這樣做,您可以使用 Visual Studio 擴充功能或命令列公用程式。You can use a Visual Studio extension or a command-line utility to do that.

您也可以使用這些工具來分析程式碼。Use these same tools to analyze your code. 從這裡下載工具 (dotnet-apiport),然後觀看此影片以了解如何使用這些工具。Download the tools here (dotnet-apiport) and then watch this video to learn how to use them.   

如果您的程式碼與標準不相容,請考慮其他可以實作該程式碼的方式。If your code isn't compatible with the standard, consider other ways that you could implement that code. 一開始先開啟 .NET API 瀏覽器Start by opening the .NET API Browser. 您可以使用該瀏覽器來檢閱 .NET Standard 2.0 中提供的 API。You can use that browser to review the API's that are available in the .NET Standard 2.0. 請務必將清單範圍限定為 .NET Standard 2.0。Make sure to scope the list to the .NET Standard 2.0.

Dot Net 選項

您部分的程式碼專屬於特定平台,必須繼續留在傳統型應用程式專案中。Some of your code will be platform-specific and will need to remain in your desktop application project.

範例:將資料存取程式碼移轉至 .NET Standard 2.0 程式庫Example: Migrating data access code to a .NET Standard 2.0 library

假設我們有一個非常基本的 Windows Forms 應用程式,它會向我們的 Northwind 範例資料庫顯示客戶。Let's assume that we have a very basic Windows Forms application that shows customers from our Northwind sample database.

Windows Forms 應用程式

專案包含內有名為 Northwind 靜態類別的 .NET Standard 2.0 類別庫。The project contains a .NET Standard 2.0 class library with a static class named Northwind. 如果將此程式碼移至 Northwind 類別,則不會進行編譯,因為它會使用 SQLConnectionSqlCommandSqlDataReader 類別,以及 .NET Standard 2.0 中未提供的類別。If we move this code into the Northwind class, it won't compile because it uses the SQLConnection, SqlCommand, and SqlDataReader classes, and those classes that are not available in the .NET Standard 2.0.

public static ArrayList GetCustomerNames()
{
    ArrayList customers = new ArrayList();

    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString =
            @"Data Source=" +
            @"<Your Server Name>\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=SSPI";

        conn.Open();

        SqlCommand command = new SqlCommand("select ContactName from customers order by ContactName asc", conn);

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                customers.Add(reader[0]);
            }
        }
    }

    return customers;
}

您可以使用 .NET API 瀏覽器尋找替代方式。We can use the .NET API Browser to find an alternative though. DbConnectionDbCommandDbDataReader 類別全都以 .NET Standard 2.0 提供,所以我們可以改為使用它們。The DbConnection, DbCommand, and DbDataReader classes are all available in the .NET Standard 2.0 so we can use them instead.

此修訂版本使用這些類別來取得客戶清單,但要建立 DbConnection 類別,就必須傳入我們在用戶端應用程式中建立的 Factory 物件。This revised version uses those classes to get a list of customers, but to create a DbConnection class, we'll need to pass in a factory object that we create in the client application.

public static ArrayList GetCustomerNames(DbProviderFactory factory)
{
    ArrayList customers = new ArrayList();

    using (DbConnection conn = factory.CreateConnection())
    {
        conn.ConnectionString = @"Data Source=" +
                        @"<Your Server Name>\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=SSPI";

        conn.Open();

        DbCommand command = factory.CreateCommand();
        command.Connection = conn;
        command.CommandText = "select ContactName from customers order by ContactName asc";

        using (DbDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                customers.Add(reader[0]);
            }
        }
    }

    return customers;
}

在 Windows Form 的程式碼後置頁面上,可以只是建立 Factory 執行個體後,再傳入我們的方法中。In the code-behind page of the Windows Form, we can just create factory instance and pass it into our method.

public partial class Customers : Form
{
    public Customers()
    {
        InitializeComponent();

        dataGridView1.Rows.Clear();

        SqlClientFactory factory = SqlClientFactory.Instance;

        foreach (string customer in Northwind.GetCustomerNames(factory))
        {
            dataGridView1.Rows.Add(customer);
        }
    }
}

建立 UWP 應用程式Create a UWP app

現在就可以開始將 UWP app 新增至您的方案。Now you're ready to add a UWP app to your solution.

傳統型轉 UWP 橋接器影像

您仍然需要使用 XAML 設計 UI 頁面,並撰寫任何裝置或平台特定程式碼。不過,一旦完成,您就可以將適用範圍涵蓋全系列的 Windows 10 裝置,而您的應用程式頁面也將具備可配合不同螢幕大小及解析度適當調整的現代化觀感。You'll still have to design UI pages in XAML and write any device or platform-specific code, but when you are done, you'll be able to reach the full breadth of Windows 10 devices and your app pages will have a modern feel that adapts well to different screen sizes and resolutions.

應用程式除了鍵盤和滑鼠之外,還能回應其他輸入機制,而且功能和設定在所有裝置上都會變得直覺。Your app will respond to input mechanisms other than just a keyboard and mouse, and features and settings will be intuitive across devices. 這表示使用者了解過一次操作方法後,不論什麼裝置,使用起來都會非常熟悉。This means that users learn how to do things one time, and then it works in a very familiar way no matter the device.

以上所述只是 UWP 附帶的其中幾個好處。These are just a few of the goodies that come with UWP. 若要深入了解,請參閱建置美好的 Windows 使用體驗To learn more, see Build great experiences with Windows.

新增 UWP 專案Add a UWP project

首先,將 UWP 專案新增至您的方案。First, add a UWP project to your solution.

UWP 專案

然後,從 UWP 專案新增 .NET Standard 2.0 程式庫專案的參考。Then, from your UWP project, add a reference the .NET Standard 2.0 library project.

UWP 方案總管窗格的螢幕擷取畫面,會呼叫 .net 專案的類別庫參考參考。

建置您的頁面Build your pages

新增 XAML 頁面,並呼叫在 .NET Standard 2.0 程式庫中的程式碼。Add XAML pages and call the code in your .NET Standard 2.0 library.

UWP app

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel x:Name="customerStackPanel">
        <ListView x:Name="customerList"/>
    </StackPanel>
</Grid>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        SqlClientFactory factory = SqlClientFactory.Instance;

        customerList.ItemsSource = Northwind.GetCustomerNames(factory);
    }
}

若要開始使用 UWP,請參閱什麼是 UWP appTo get started with UWP, see What's a UWP app.

將目標範本擴及 iOS 和 Android 裝置Reach iOS and Android devices

您可以透過新增 Xamarin 專案,將目標範本擴及 Android 和 iOS 裝置You can reach Android and iOS devices by adding Xamarin projects.

顯示適用于 Xamarin 應用程式的 Android 裝置和 i O S 裝置的影像。

這些專案允許您使用 C# 來建置可以完整存取平台特定及裝置特定 API 的 Android 和 iOS 應用程式。These projects let you use C# to build Android and iOS apps with full access to platform-specific and device-specific APIs. 這些應用程式充分利用平台特定的硬體加速,並且針對原生效能進行編譯。These apps leverage platform-specific hardware acceleration, and are compiled for native performance.

這些應用程式可以存取基礎平台及裝置公開的各種功能 (包括 iBeacons 和 Android Fragments 等平台特定功能),而您將會使用標準原生使用者介面控制項來建置使用者所預期之外觀與風格的 UI。They have access to the full spectrum of functionality exposed by the underlying platform and device, including platform-specific capabilities like iBeacons and Android Fragments and you'll use standard native user interface controls to build UIs that look and feel the way that users expect them to.

就像 UWP 一樣,新增 Android 或 iOS 應用程式的成本較低,因為您可以重複使用 .NET Standard 2.0 類別庫中的商務邏輯。Just like UWPs, the cost to add an Android or iOS app is lower because you can reuse business logic in a .NET Standard 2.0 class library. 您必須使用 XAML 設計 UI 頁面,並撰寫任何裝置或平台特定的程式碼。You'll have to design your UI pages in XAML and write any device or platform-specific code.

新增 Xamarin 專案Add a Xamarin project

首先,將 [Android][iOS][跨平台] 專案新增至您的方案。First, add an Android, iOS, or Cross-Platform project to your solution.

您可以在 [加入新的專案] 對話方塊的 [Visual C#] 群組底下找到這些範本。You can find these templates in the Add New Project dialog box under the Visual C# group.

[加入新專案] 對話方塊的螢幕擷取畫面,其中顯示已安裝的 > Visual C 清晰選取,以及已呼叫的 Android、跨平臺和 i O S 選項。

注意

跨平台專案非常適合只有極少平台特定功能的應用程式。Cross-platform projects are great for apps with little platform-specific functionality. 您可以使用這些專案建立一個在 Android、iOS 及 Windows 上執行的原生 XAML 型 UI。You can use them to build one native XAML-based UI that runs on iOS, Android, and Windows. 在此深入了解。Learn more here.

接著,從 Android、iOS 或跨平台專案中新增類別庫專案的參考。Then, from your Android, iOS, or cross-platform project, add a reference the class library project.

方案總管窗格的螢幕擷取畫面,會呼叫 Android、i O S 或跨平臺專案的類別庫參考參考。

建置您的頁面Build your pages

我們的範例會在 Android 應用程式中顯示客戶清單。Our example shows a list of customers in an Android app.

Android 應用程式

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" android:textSize="16sp"
    android:id="@android:id/list">
</TextView>
[Activity(Label = "MyAndroidApp", MainLauncher = true)]
public class MainActivity : ListActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SqlClientFactory factory = SqlClientFactory.Instance;

        var customers = (string[])Northwind.GetCustomerNames(factory).ToArray(typeof(string));

        ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, customers);
    }
}

若要開始使用 Android、iOS 和跨平台專案,請參閱 Xamarin 開發人員入口網站To get started with Android, iOS, and cross-platform projects, see the Xamarin developer portal.

接下來的步驟Next steps

尋找您的問題解答Find answers to your questions

有任何問題嗎?Have questions? 請在 Stack Overflow 上發問。Ask us on Stack Overflow. 我們的團隊會監視這些標籤Our team monitors these tags. 您也可以在這裡發問。You can also ask us here.

提供意見反應或功能建議Give feedback or make feature suggestions

請參閱 UserVoiceSee UserVoice.