WIN10/UWP new feature series-launcher for communication between applications

Source: Internet
Author: User
Tags app service

In the UWP, Microsoft has added a lot of features to the Windows.System.Launcher launcher, which used to start the app only, open the specified extension file, parse the URI protocol, and prompt for store downloads when the launched app is not installed.

Today, Microsoft enriches the functionality of launcher, using the new launcher we can implement in the app call File Explorer, App-to-app Server (app to app service), Background Task server app ( Background task processing Service app) There are also settings page calls.

One:launcher.launchfolderasync

The method can open the specified folder with two method overloads:

1  Public Static Iasyncoperation<system.boolean> Launchfolderasync (Istoragefolder folder); 2  Public Static Iasyncoperation<system.boolean> Launchfolderasync (Istoragefolder folder, folderlauncheroptions options);

For example, we can open the picture folder of the device in the app

var await Launcher.launchfolderasync (knownfolders.pictureslibrary);

KnownFolders provides us with a lot of accessible folder locations

When attempting to access these folders, we also need to declare the appropriate permissions in Package.appxmanifest

1 < Uap:capabilityname = "Pictureslibrary" />
View Code

Two: Launcher.launchuriforresultsasync

My personal habits are called: Apptoapp communication between applications

New Launchuriforresultsasync api,windows applications (as well as Windows Web Apps) can start and exchange data and files with each other. With this new API, complex tasks that previously required multiple apps can now be processed seamlessly, making it impossible for users to feel the switch between apps at all. For example, we can start a social app to select a contact or apply a payment scenario.

If you want to use Apptoapp, first we need to declare the protocol service in the app that is launched:

1   <Applications>2     <ApplicationId= "APP"3       4 ...5  6 <!--protocols that are registered for processing in apps that provide external calling services7 in the protocol, Returnresults has three possible values8        9 optional applications can be started by using Launchuriforresultsasync for processing results, rather than launchuriasync. Ten when using optional, the calling app must explicitly assume that it is an app that can be called for results (confirmed to be Apptoapp mode), and we can also check One the OnActivated () event parameter is determined. If the Iactivatedeventargs.kind property value of the event argument is Protocolforresults, or A The event argument type is Protocolactivatedeventargs, you can determine that the app was started by Launchuriforresultsasync.  -                        - the Always app can only be called through the Apptoapp mode, that is, it can only be called through Launchuriforresultsasync the           - The None application cannot be called through Apptoapp, that is, it can only be called by Launchuriasync--> -       <Extensions> -         <uap:extensionCategory= "Windows.protocol"> +           <Uap:protocolName= "App2app-sample"Returnresults= "Optional"> -             <Uap:displayname>App2app Sample</Uap:displayname> +           </Uap:protocol> A         </uap:extension> at       </Extensions> -     </Application> - </Applications>

Then rewrite the onactivated method in App.xaml.cs

1 protected Override voidOnActivated (Iactivatedeventargs args)2 {3     if(args. Kind = =activationkind.protocolforresults)4     {5Frame Rootframe = Window.Current.Content asFrame;6         if(Rootframe = =NULL)7         {8Rootframe =NewFrame ();9Window.Current.Content =Rootframe;Ten         } One         varProtocolargs =(Protocolforresultsactivatedeventargs) args; ARootframe.navigate (typeof(app2apppage), Protocolargs); - Window.Current.Activate (); -     } the}

In the above code we need to check if the value of Iactivatedeventargs.kind is protocolforresults to determine if the application was started using Apptoapp mode. If it is, jump to the page app2apppage that provides the service and pass the data parameters.

In the App2apppage page we override the Onnavigatedto method to receive data, and the Onnavigationto method NavigationEventArgs contains data from the caller application.

Ok, start rewriting the Onnavigatedto method

1 protected Override voidonnavigatedto (NavigationEventArgs e)2 {3     varProtocolforresultsargs = E.parameter asProtocolforresultsactivatedeventargs;4_operation =protocolforresultsargs.protocolforresultsoperation;5     if(ProtocolForResultsArgs.Data.ContainsKey ("TestData"))6     {7         varDatafromcaller = protocolforresultsargs.data["TestData"] as string;8         //page displays the resulting data9Testdata.text =Datafromcaller;Ten     } One}

When the app finishes processing the data, you can call protocolforresultsoperation. The Reportcompleted method returns a Valueset object to the caller app

1 Private voidButton_Click (Objectsender, RoutedEventArgs e)2 {3     varresult =NewValueSet ();4result["Returndata"] =string. Format ("He gave you {0}", Money. Text);5 _operation. reportcompleted (result);6}

At this point, the callee app logic is complete

Return to our callers, call APP2 using the Launcher.launchuriforresultsasync method, and let APP2 help with the logic.

1 Private Asynctask<string>getlaunchappforresults ()2    {3        stringResulttxt =string. Empty;4        //Callee 's Protocol Name5        varTestappuri =NewUri ("app2app-sample:");6  7        varOptions =Newlauncheroptions8        {9            //this stuff is the targetapplicationpackagefamilyname of the callee.Ten            //You can use the Windows.ApplicationModel.Package.Current.Id.FamilyName method in the callee to get One            //of course, the callee can also store this value in a shared folder for easy access by other apps A            //to use a shared folder, you can use the Sharestorageaccessmanager class to handle -            //Sharestorageaccessmanager will explain to you later -Targetapplicationpackagefamilyname ="0DE26E7D-8C3D-4040-8275-93A92570666D_MD3S7CN435NW2" the        }; -   -        //the data valueset to be passed cannot exceed 100k maximum -        varInputdata =NewValueSet (); +inputdata["TestData"] ="100 bucks Don't give me, hit me also want to call people ... Party Block"; -   +        //start APP2 to provide services ALaunchuriresult result =awaitWindows.System.Launcher.LaunchUriForResultsAsync (Testappuri, Options, inputdata); at   -        //Processing of results -        if(result.) Status = = Launchuristatus.success && -Result. Result! =NULL&& -Result. Result.containskey ("Returndata")) -        { in            varThevalues =result. Result; -Resulttxt = thevalues["Returndata"] as string; to        } +        returnResulttxt; -}

We imitate Little Red Riding Hood (APP1) to the uncle for money, Qaq to see the effect:

Recommend a UWP development group: 53078485 You can come in together to learn ~ ~

WIN10/UWP new feature series-launcher for communication between applications

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.