New Fashion Windows 8 Development (10): How to share text content

Source: Internet
Author: User
Tags blank page

In this blog, http://blog.csdn.net/tcjiaan.

 

First, you can see the following figure.

 

 

I believe in this function of Weibo. So how is it implemented?

First of all, we need to understand something, such as application protocols and names, which are hard to understand and translate. So let's see where it is set up. Maybe you will feel a little bit better.

After you use vs2012 to create a Windows Store application, you will see a configuration file in the project to configure information related to the application package.

Double-click it.

Switch to the declaration tab.

 

This is the application protocol. You can understand that your application declares multiple startup methods and uses them as specific targets, as shown in the figure above, why can Sina Weibo be started in other applications and share information? In this way, we can understand data transmission between applications, the most common method is copy-paste, which uses the clipboard as the carrier to share data among applications.

 

If you still think it is abstract, the best way is to use an example to describe how to simulate the functions of Sina Weibo. For a solution, we need to build two projects, one is to share the source, that is, the sender of the shared data. The other is the sharing target, that is, the recipient of the shared data.

 

First, we will implement the shared source, that is, the sender.

1. Start Vs and create a project.

2. Enter the following XAML code in mainpage. XAML.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel> <textbox name = "txtcontent" Height = "285"/> <button margin =, 0, 0 "content =" share "Click =" onshare "/> </stackpanel> </GRID>

 

3. Then, we need to process the Click Event of the button.

        private void onShare(object sender, RoutedEventArgs e)        {            DataTransferManager.ShowShareUI();        }

The showworker UI method is static and can be called directly. datatransfermanager is located in the namespace windows. ApplicationModel. datatransfer. It is used to send shared data.

So when will we set the data to be sent? For example, now I want to share a text message to the target, and set the data to be processed in the datarequested event of the datatransfermanager class.

Use the getforcurrentview method to obtain the instance of datatransfermanager. Therefore, add the following code to the constructor of the mainpage class.

        public MainPage()        {            this.InitializeComponent();            DataTransferManager.GetForCurrentView().DataRequested += MainPage_DataRequested;        }

4. process the datarequested event.

Void mainpage_datarequested (datatransfermanager sender, datarequestedeventargs ARGs) {var defl = args. request. getdeferral (); // set the data packet datapackage dp = new datapackage (); DP. properties. title = "shared text"; DP. properties. description = "share some strings. "; DP. settext (txtcontent. Text); args. Request. Data = DP; // report operation completed defl. Complete ();}

 

OK. Now I want to complete the sharing target.

In Solution Explorer, right-click the solution node and choose add> new project from the shortcut menu. Then, create an app, which is the data receiver.

1. Open the inventory file, switch to the "Declaration" tab, select "share target" from the drop-down list, and click "add.

On the right panel, find the data format section and click Add.

Because you only need to pass text information, Enter text.

Save and close the configuration file.

 

2. Create a blank page named sharedpage. XAML. The XAML code is as follows.

    <Grid Background="#FF0B4C81">        <TextBlock Margin="10,15,0,0"                   FontSize="28"                   VerticalAlignment="Top"                   HorizontalAlignment="Left"                   x:Name="tbShareText"/>    </Grid>

3. Open sharedpage. XAML. CS

Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; using Windows. applicationModel. core; // The "blank page" template is available in Http://go.microsoft.com/fwlink? Linkid = 234238 introduces namespace sharetargetsample {/// <summary> /// it can be used by itself or navigate to a blank page inside the frame. /// </Summary> Public sealed partial class sharedpage: Page {public sharedpage () {This. initializecomponent () ;}/// <summary> /// call when this page is to be displayed in the frame. /// </Summary> /// <Param name = "E"> describes how to access event data on this page. Parameter // properties are usually used on the configuration page. </Param> protected override void onnavigatedto (navigationeventargs e) {If (coreapplication. properties. containskey ("value") {This. tbsharetext. TEXT = coreapplication. properties ["value"]. tostring ();}}}}

 

We do not obtain shared data on this page. This page is only for display. The shared data we obtained is saved in the coreapplication. properties set, and the page retrieves and displays the data from the set.

 

4. Open app. XAML. CS

The onsharetargetactivated method is rewritten in the app class. If the application is started by the user, the onlaunched method is called. However, if the application is started due to a shared source call, the onsharetargetactivated method is called, this is the role of application protocols.

Protected async override void onsharetargetactivated (sharetargetactivatedeventargs ARGs) {var OPR = args. shareoperation; // start to retrieve data OPR. reportstarted (); datapackageview Pv = OPR. data; If (Pv. contains (standarddataformats. text) {coreapplication. properties ["value"] = await PV. gettextasync ();} OPR. reportdataretrieved (); frame root = Window. current. content as frame; If (root = NULL) {root = new fram E (); root. navigate (typeof (sharedpage);} window. current. content = root; window. current. activate (); // OPR. reportcompleted (); // OPR. reporterror ("cannot be shared. ");}

Okay, but we have to start and run the shared target program normally. Otherwise, it will not be installed in the Application List of the system. In Solution Explorer, select the shared target project, right-click, and choose debug> Start new instance ".

After the program runs, end it. Then, open the computer settings (move the mouse to the upper right or lower right corner, and click "set" from the sidebar that appears ", click "more computer settings") and select "share ",
Now we can see that the sharing Function of the sharing target has been enabled.

Now, run the Shared Source Project and enter some content on the page. Click share.

 

Then click our application,

 

In this way, our sharing target sample process will receive the shared data.

 

 

You may have noticed that there are two lines of code in APP. XAML. CS above that I intentionally commented out. Now let's take a look at what will happen when the two lines of code are canceled separately.

1. Cancel the first line

OPR. reportcompleted (); // opr. reporterror ("cannot be shared. ");

 

Then, run as shown in the preceding method. We find that the shared target exits as soon as it receives the data. Right, in the shared target, we process the received shared data, after processing, once the sharing operation is completed by calling the reportcompleted method of operation, the sharing target program will automatically exit.

 

In the same way, comment out the first line and cancel the comment of the second line. We fail to share the comment.

// Opr. reportcompleted (); opr. reporterror ("cannot be shared. ");

Remember, now you have to run the shared target program in normal startup mode. Because of this, new modifications will take effect.

After an error is reported, you will receive the above prompt.

 

 

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.