使用 Portable Class Library(可移植类库)开发 Universal Windows App

今天在这里跟大家聊聊关于 Windows Universal 应用夸平台的问题,首先Universal Windows App的定义相信大家已经有所了解了(如果你是一个刚刚接触 Universal APP 的开发这个请先阅读一下我之前的文章 Windows Phone 8.1 开发技术概览 [Universal APP]), 相信大家在这里最苦恼的事情莫过于在不同开发架构下分享代码了,今天我在这里给大家推荐一个解决方案使用可移植类库(Portable Class Library)在不同的Windows项目之间分享代码。(Windows 8.1 / Windows Phone 8.1 )这里还包括Silverlight开发的 Windows Phone 8.1 应用。

首先介绍一下什么是 Portable Class Library,称之为‘可移植类库’(简称PCL)支持 C# 语言开发,并且在开发 Universal 类库时支持UI呈现。 这里在次强调一下目前只支持C#开发,C# 的语法可以在 PCL 中使用,方便C# 的开发人员快速上手,并且支持调用 Windows Runtime 的 SDK 例如,网络访问,JSON 处理,内容分享等功能。 也非常适合三方SDK开发和功能集成。

如何创建一个 PCL 的类库呢非常简单只需要打开我们的VS2013(update 2 及以上版本)选择Universal 应用模板 选择PCL的项目模板即可,(Component支持 Universal 应用。 DLL 支持Silverlight以及 Windows XAML C# 应用)这里最大的区别是 DLL 的类库不允许类库中使用UI内容。(原因非常简单 Universal 应用和 SL的应用架构不同)

image

通过项目属性 我们还可以通过 Output Type 来切换项目的输出类型(Component 或 DLL)

image

另外 我们还可以是通过 Targets 属性来适配应用的应用适配平台,这里要注意的是如果应用跨 Universal 和 Silverlight 平台 (8.0 和 8.1)类库内容会受到很大的影响(类库版本越旧我们在PCL中可以使用的WinRT feature 也就越少),并且PCL 将不能支持UI控件的分享。

image

 

上面提到各种限制肯能有些复杂,我用一张图来给大家说明一下。(这里针对Universal 8.1 APP 和 Silverlight 应用架构)

image

1.如果你的类库只想被Universal 应用调用,那么你需要选择 Windows Runtime Component 进行输出,你的PCL将支持大部分 Windows RT的 Feature 并且支持UI控件的分享,但是WinJS项目不支持 UI 控件的展示,这里原因很简单 XAML上层渲染和 HTML是不同的。

2.如果你需要你的PCL支持 Silverlight 项目的调用,那么你需要选择 Class Library (DLL)进行输出,你的PCL也可以支持大部分 Windows RT的 Feature 。但是不可以进行 UI控件的分享,并且你输出的DLL将不能被 Universal APP的 C++ XAML 和 HTML WinJS 项目调用。

这里最好的建议就是相同的类库,如果想让它同时兼容 Universal 架构(XAML C++/C#  和 HTML WinJS)Silverlight架构,只需要将PCL的输出类型切换在编译一次就可。(三方SDK建议这样做:))

我这里给大家一个测试代码是使用 WinRT中的Share Contract 进行应用间分享。(因为没有 UI 内容可以直接 Target 到 Universal 和 Silverlight 项目中去,当然是两次编译)

项目结构(为了方便这里我做了 Component 和 DLL 的项目但是项目中的代码是相同的,当然在开发的时候用link的形势也可以)

image

PCL 分享类库代码如下

public sealed class ShareText
    {
        private DataTransferManager dataTransferManager;

        public string DataContent { get; set; }

        public ShareText()
        {
            this.dataTransferManager = DataTransferManager.GetForCurrentView();
            this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
            DataContent = "Share Text From PCL";
        }
        private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
        {
            // Call the scenario specific function to populate the datapackage with the data to be shared.
            if (GetShareContent(e.Request))
            {
                // Out of the datapackage properties, the title is required. If the scenario completed successfully, we need
                // to make sure the title is valid since the sample scenario gets the title from the user.
                if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
                {
                    return;
                }
            }
        }
        public bool GetShareContent(DataRequest request)
        {
            bool succeeded = false;

            string dataPackageText = DataContent;
            if (!String.IsNullOrEmpty(dataPackageText))
            {
                DataPackage requestData = request.Data;
                requestData.Properties.Title = "Share Text";
                requestData.Properties.Description = "Share Description"; // The description is optional.
                //requestData.Properties.ContentSourceApplicationLink = GetType().Name;
                requestData.SetText(dataPackageText);
                succeeded = true;
            }
            else
            {
                request.FailWithDisplayText("Enter the text you would like to share and try again.");
            }
            return succeeded;
        }
        public void ShowShareUI()
        {
            // If the user clicks the share button, invoke the share flow programatically.
            DataTransferManager.ShowShareUI();
        }
    }

C# 项目调用

private void Button_Click(object sender, RoutedEventArgs e)
        {
            UniversalComponent.ShareText st = new UniversalComponent.ShareText();

            st.DataContent = "Hello PCL form C#";

            st.ShowShareUI();
        }

C++  项目调用

void UniversalC__App::BlankPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    UniversalPCL::ShareText^ ST = ref new UniversalPCL::ShareText;
    ST->DataContent = "Hello PCL from C++";
    ST->ShowShareUI();
}

HTML + WinJS项目调用

function callComponent() {
var component = new UniversalPCL.ShareText();
    component.dataContent = "Hello form JS";
    component.showShareUI();
}

Silverlight C# 调用

private void Button_Click(object sender, RoutedEventArgs e)
        {
            UniversalComponent.ShareText st = new UniversalComponent.ShareText();

            st.DataContent = "Hello PCL form SL";

            st.ShowShareUI();
        }

我们可以在 VS 中测试任意一个平台的调用情况

image

这里我就不逐一展示测试效果了,贴一张 C++ 调用的截图让大家过过瘾也好 :)

image    image  image

希望上的总结可以帮助到大家, 同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick

posted @ 2014-11-30 18:12  王博_Nick  Views(5861)  Comments(2Edit  收藏  举报