Development of New Fashion Windows 8 (11): sharing target example

Source: Internet
Author: User
Tags blank page

Old Zhou's blogHttp://blog.csdn.net/tcjiaan, Reprinted please indicate the original author and source.

 

PreviousArticleIn, we blew up the problem of sharing data, and we will also give a simple example later. In this article, we will give a comprehensive example to make a sharing all-around goal, the target application can receive data in various formats.

First, let's get to know a class that tells us what standard data is -- standarddataformats, which is a static class located in windows. applicationModel. under datatransfer, it means that all its members are static.

A series of static members have already told us six standard data formats. I believe you don't need to introduce them, but you will know the names. Storageitems is generally a file stored in a computer.

 

In this example, five protocols (except the RTF Protocol) are enabled to accept multiple types of shared data. Let's take a look at the running results first.

Scenario 1: Start the Bing application, search for some images, select one of them, move the mouse to the upper right or lower right corner of the screen, and select "share" after the sidebar appears ".

 

 

 

Scenario 2: open a browser and open a page. You can obtain the page Uri.

 

 

 

 

Scenario 3: Open the "Netease cloud read" application, open an article at will, share it, and get the shared string.

 

 

 

 

The implementation steps are as follows:

1. Start Vs and create a new project. 45 words are omitted here.

2. Open the package. appxmanifest file, switch to Declaration, select share target in the list, and click Add.

 

In the framework on the right, add support for five data formats.

 

3. Open mainpage. XAML and enter the following XAML.

<Page X: class = "MyApp. mainpage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Local =" using: MyApp "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: MC =" http://schemas.openxmlformats.org/markup-compatibility/2006 "MC: ignorable = "D"> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <canvas name = "cvtext" visibility = "Collapsed"> <textbox name = "txttext" canvas. left = "24" canvas. top = "12" width = "860" Height = "450" textwrapping = "Wrap"/> </canvas> <canvas name = "cvbitmap" visibility = "Collapsed"> <image name = "IMG" canvas. top = "20" canvas. left = "24" stretch = "Uniform" width = "900"/> </canvas> <textblock name = "tbfomats" verticalignment = "bottom" horizontalalignment = "Left" fontsize = ""24" canvas. zindex = "10" textwrapping = "Wrap"/> </GRID> </Page>

4. Open mainpage. XAML. CS and rewrite onsharetargetactivated for the app class. To be nice, define another category.

 
Public sealed partial class app: Application {protected override void onsharetargetactivated (windows. applicationModel. activation. sharetargetactivatedeventargs ARGs) {condition operation op = args. repeated operation; frame root = Window. current. content as frame; If (root = NULL) {root = new frame (); root. navigate (typeof (mainpage), OP);} window. current. content = root; window. current. activate ();}}

5. Remaining mainpage. XAML. CSCodeAs follows.

Using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; using Windows. applicationModel. datatransfer; using Windows. applicationModel. datatransfer. sharetarget; using Windows. storage; using Windows. storage. streams; namespace MyApp {// <summary> /// you can use it to navigate to a blank page in the frame. /// </Summary> Public sealed partial class mainpage: Page {public mainpage () {This. initializecomponent ();} protected async override void onnavigatedto (navigationeventargs e) {If (E. parameter is specified operation) {parameter operation op = (parameter operation) E. parameter; datapackageview Pv = op. data; // display the ireadonlylist containing format <string> listformats = PV. availableformats; string formatsstr = string. join ("\ t", listformats. toarray (); tbfomats. TEXT = formatsstr; // process if (Pv. contains (standarddataformats. bitmap) {randomaccessstreamreference strref = await PV. getbitmapasync (); irandomaccessstreamwithcontenttype stream = await strref. openreadasync (); // read the image windows. UI. XAML. media. imaging. bitmapimage BMP = new windows. UI. XAML. media. imaging. bitmapimage (); BMP. setsource (Stream); this. IMG. source = BMP; cvtext. visibility = windows. UI. XAML. visibility. collapsed; cvbitmap. visibility = windows. UI. XAML. visibility. visible;} else if (Pv. contains (standarddataformats. text) {string value = await PV. gettextasync (); txttext. TEXT = value; cvbitmap. visibility = windows. UI. XAML. visibility. collapsed; cvtext. visibility = windows. UI. XAML. visibility. visible;} else if (Pv. contains (standarddataformats. uri) {URI xuri = await PV. geturiasync (); txttext. TEXT = xuri. tostring (); cvbitmap. visibility = windows. UI. XAML. visibility. collapsed; cvtext. visibility = windows. UI. XAML. visibility. visible;} else if (Pv. contains (standarddataformats. html) {string html = htmlformathelper. getstaticfragment (await PV. gethtmlformatasync (); txttext. TEXT = HTML; cvtext. visibility = windows. UI. XAML. visibility. visible; cvbitmap. visibility = windows. UI. XAML. visibility. collapsed;} else if (Pv. contains (standarddataformats. storageitems) {ireadonlylist <istorageitem> items = await PV. getstorageitemsasync (); string files = string. empty; foreach (VAR item in items) {files + = item. name + "\ n";} txttext. TEXT = files; cvbitmap. visibility = windows. UI. XAML. visibility. collapsed; cvtext. visibility = windows. UI. XAML. visibility. visible ;}}}}

In the code above, the operation is passed to the home page as a navigation parameter and obtained from onnavigatedto in mainpage.

 

 

Okay, that's it. 88

 

Related Article

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.