Develop Universal Windows APP using the Portable class library (portable libraries)

Source: Internet
Author: User
Tags silverlight

Today I'm here to talk to you about Windows Universal app boast platform, first Universal the definition of Windows app I believe you already know something (if you are a just touch Universal APP The development of this please read my previous article on the Windows Phone 8.1 Development Technology Overview [Universal APP]), I believe that everyone here is the most distressed to share the code under different development architectures, Today I'm here to recommend a solution to share code between different Windows projects using portable class libraries (portable Class library). (Windows 8.1/windows Phone 8.1) also includes a Silverlight-developed Windows Phone 8.1 app.

First, what is the Portable class library, called the ' Portable Class Library ' (PCL), supports C # language development, and supports UI rendering when developing Universal class libraries. Here's a little bit of emphasis. Currently only supports C # development, C # syntax can be used in the PCL, easy for C # developers to get started quickly, and support for invoking the SDK of Windows Runtime such as network access, JSON processing, content sharing and other functions. It is also ideal for Tri-party SDK development and functional integration.

How to create a PCL class library It's very simple just open our VS2013 (update 2 and later) Select the Universal application template to select the PCL project template, (component supports Universal apps. DLL supports Silverlight and Windows XAML C # apps ) The biggest difference here is that the DLL's class library does not allow UI content to be used in class libraries . (The reason is very simple Universal application and SL application architectures are different)

Through the project properties we can also toggle the output type of the project (Component or DLL)

In addition, we can also use the Targets property to adapt to the application of the application platform, it is important to note that if the application across the Universal and Silverlight platform (8.0 and 8.1) class library content will be greatly affected ( The older the class library version, the less WINRT feature we can use in the PCL , and the PCL will not be able to support the sharing of UI controls .

The above mentioned restrictions can be somewhat complicated, I use a picture to explain to you. (Here for Universal 8.1 app and Silverlight application architecture)

1. If your class library only wants to be called by the Universal app, then you need to choose Windows Runtime Component to output, your PCL will support most of Windows RT's Feature and support UI control sharing, However, the WINJS project does not support the presentation of UI controls, for the simple reason that XAML upper-layer rendering and HTML are different.

2. If you need your PCL support for Silverlight project calls, then you need to select the Class Library (DLL) for the output, and your PCL can support most Windows RT Feature. However, the UI control cannot be shared, and the DLL you output will not be called by the Universal app's C + + XAML and HTML WinJS project.

The best advice here is the same class library, and if you want it to be compatible with both the Universal schema (XAML c++/c# and HTML WinJS) Silverlight schemas, simply switch the PCL output type to compile once. (The three-party SDK recommends this:))

I'm here to give you a test code for sharing between applications using the share contract in WINRT. (because no UI content can be directly Target to the Universal and Silverlight projects, of course, two compilations)

Project structure (for convenience here I did Component and DLL project but the code in the project is the same, of course, in the development of the link in the situation can also be)

The PCL share class library code is as follows

 Public Sealed classSharetext {PrivateDatatransfermanager Datatransfermanager;  Public stringdatacontent {Get;Set; }  PublicSharetext () { This. Datatransfermanager =Datatransfermanager.getforcurrentview ();  This. datatransfermanager.datarequested + =NewTypedeventhandler<datatransfermanager, Datarequestedeventargs> ( This.            ondatarequested); DataContent="Share Text from PCL"; }        Private voidondatarequested (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 scenario gets the title from the user.                if(String.IsNullOrEmpty (e.request.data.properties.title)) {return; }            }        }         Public BOOLgetsharecontent (datarequest request) {BOOLsucceeded =false; stringDatapackagetext =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 would like to share and try again."); }            returnsucceeded; }         Public voidShowshareui () {//If The user clicks the Share button, invoke the share flow programatically.Datatransfermanager.showshareui (); }    }

C # project calls

Private void Button_Click (object  sender, RoutedEventArgs e)        {            new  Universalcomponent.sharetext ();             " Hello PCL Form C # " ;            St. Showshareui ();        }

C + + Project invocation

void Universalc__app::blankpage::button_click (platform::object^ sender, windows::ui::xaml::routedeventargs^ e) {    Universalpcl::sharetext ref New Universalpcl::sharetext;    ST"Hello PCL from C + +";    ST---Showshareui ();}

HTML + WINJS Project call

function callcomponent () {
var New Universalpcl.sharetext ();     = "Hello form JS";    Component.showshareui ();}

Silverlight C # calls

Private void Button_Click (object  sender, RoutedEventArgs e)        {            new  Universalcomponent.sharetext ();             " Hello PCL Form SL " ;            St. Showshareui ();        }

We can test the invocation of any platform in VS

Here I do not show the test results, paste a C + + call to let everyone enjoy it:)

Hope the summary can help everyone, at the same time welcome everyone here and I communicate or on Sina Weibo @ Wang Bo _nick

Develop Universal Windows APP using the Portable class library (portable libraries)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.