65.9K
CodeProject 正在变化。 阅读更多。
Home

Windows 通用应用中的代码共享

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (4投票s)

2016年3月20日

CPOL

2分钟阅读

viewsIcon

12745

downloadIcon

76

在本文中,我们将学习如何使用 Windows 通用应用开发在 Windows 应用商店应用和 Windows 手机应用之间共享代码。

引言

首先,我们来谈谈什么是通用应用?Windows 通用应用是用 Windows 8.1 或 Windows 10 代码库编写的软件应用程序,它不仅可以在 Windows PC 上运行,还可以在 Windows 平板电脑、智能手机、智能手表或 Xbox 上运行。但这并不意味着相同的可执行文件将在所有这些环境中运行。Windows 手机和 Windows PC 使用不同类型的文件来执行,因此通用应用为在两种环境中执行提供不同的输出文件,但代码库是相同的。我们可以使用代码共享为所有 Windows 环境创建应用。

通用应用开发

假设您的系统中已经安装了 Windows 8.1 开发环境。如果没有,请从这里下载 SDK。

创建一个新的项目,即空白应用(通用应用),并为其命名为你想要的名称。

在解决方案资源管理器中查看时,您会发现三种类型的项目:Windows 应用商店应用、Windows 手机应用和共享项目。Windows 应用商店应用将创建在 Windows PC 上运行的输出文件,Windows 手机应用将创建在 Windows 手机上运行的输出文件,共享项目将共享将在两个前述应用中使用的代码。在这里,我们共享代码,但不共享设计,因此我们将为每种类型的应用在 XAML 中单独创建设计。

您会看到,MainPage.xaml 及其代码文件包含在两个项目中(Windows 应用商店和 Windows 手机),而 App.xaml 包含在共享项目中。现在我们将共享 MainPage.xaml 的代码文件。因此,在共享项目中添加一个与 MainPage.xaml 代码文件同名的类,即 MainPage.cs。使其为部分密封类并继承自 Page 类。

//
// MainPage.cs in shared project
//
using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI.Xaml.Controls;
namespace VilleIT_UniversalAppDemo
{
    public sealed partial class MainPage : Page
    {
    }
}

将 Windows 手机应用文件和 Windows 应用商店应用文件的所有代码移动到共享代码文件,但除构造函数外。

//
// MainPage.cs in shared project
//
using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace VilleIT_UniversalAppDemo
{
    public sealed partial class MainPage : Page
    {
        /// 

        /// Invoked when this page is about to be displayed in a Frame.
        /// 

        /// Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }
    }
}

在两个应用的两个 XAML 页面中添加一个按钮和一个文本块。

//
// MainPage.xaml in windows store and windows phone app
//
        <button width="200" height="100" foreground="Black" background="White" content="Say Hello!"
            name="btnSayHello" horizontalalignment="Center" fontsize="30" click="btnSayHello_Click"/>
        <textblock name="txtMessage" foreground="Yellow" fontsize="30" horizontalalignment="Center" margin="20"/>

在共享代码文件中创建点击事件处理程序,该处理程序将被两个应用的按钮调用。

//
// MainPage.cs in shared project
//
using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace VilleIT_UniversalAppDemo
{
    public sealed partial class MainPage : Page
    {
        /// 

        /// Invoked when this page is about to be displayed in a Frame.
        /// 

        /// Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           
        }
        private void btnSayHello_Click(object sender, RoutedEventArgs e)
        {
            txtMessage.Text = "This is hello message";
        }
    }
}

输出

© . All rights reserved.