Blog Park Client UAP Development Essay--building the bridge between apps

Source: Internet
Author: User
Tags email account

The students who developed the Windows Phone app should have noticed that Windows Phone has more restrictions on the app for security purposes. I remember a 360 of the students very helpless said: WP is too safe, we do WP on the 360 defender basically nothing to do. But when WP360 that app comes out, there are still a lot of users installed it, although it is really not very helpful to security, but it is helpful for users to know their phone usage. One of the users of the review is: from Android to PC, I have been using 360, so on WP I also use. Oh, fan Ah! In fact, he does not care about iron powder.

It's far away! Go back to the topic of this essay: Our app only has access to its own folders and limited public folders, but not to other apps ' folders, which is called sandboxing. So what if we want to call them? There is still a way to do that.

Today we will introduce the 2 main methods of calling between apps:

Protocol Call

Simple is to use the Windows.System.launcher class, through a shape such as "mailto:[email protected," such as the protocol prefix mailto, colon, and a string consisting of a URI, Call the application that registered the protocol prefix (here is mailto) and pass the entire URI to the process it handles.

For example, this code:

await Launcher. Launchuriasync ( the new Uri("mailto:[email protected ]"));

After the operation will be transferred to our mail application to [email protected] This email account e-mail:

Isn't it simple?

Of course, this is just a call to the application of the system. We can also invoke a third-party application that has registered the corresponding protocol prefix, and can register a protocol prefix for another application.

So how to register it?

Double-click our app's Package.appxmanifest file, select the protocol in the Declarations panel, click Add:

In general, simply fill in the name with the protocol prefix that we wish to register. This prefix is best for our application, such as CNBLOGUAP. Also on Windows and Windows Phone platforms, the protocol prefix is somewhat limited. For example, on the Windows Phone, even if our app registers the mailto protocol prefix, the system will still automatically invoke the system's mail application. Detailed information can be found in https://msdn.microsoft.com/zh-CN/library/windows/apps/xaml/hh779670. After registering the protocol prefix, our application can be called by a URI that is shaped like "cnbloguap:xxxx".

So how do you handle the URI that invokes our application?

This requires us to override the "OnActivated" method in App.xaml.cs. The code is as follows:

Protected Async override void OnActivated (iactivatedeventargs args)

{

if(args. Kind = = Activationkind. Protocol)

{

Protocolactivatedeventargs Protocolargs = args as Protocolactivatedeventargs;

//todo:handle URI activation

//The received URI is EventArgs.Uri.AbsoluteUri

}

}

The full Uri of the "cnbloguap:xxxx" can be obtained by Protocolargs.uri . The next thing to do is to do whatever you want.

It is important to note that it is best to add the "OnLaunched" method of the Start app section of the code, or the application finished processing the URI will be directly exited. You can refer to the example on MSDN: https://code.msdn.microsoft.com/windowsapps/Association-Launching-535d2cec/, In this example, this part of the code is extracted into the Createrootframe method.

File invocation

Protocol calls can be inconvenient if we need to transfer more information between applications. Then the file invocation will be useful.

For example, there is a scenario where we download a song Myfavorite.mp3 and put it in the music catalogue and listen to it immediately.

Then you can add the following fragment to our code:

var music = await knownfolders. Musiclibrary.getfileasync ("Myfavorite.mp3");

await Launcher. Launchfileasync (music);

The system will automatically call the music player to play the song. But one thing to note is that when the player plays the song once, we have some time to access this MP3 file will be an error t.t

As with protocol calls, we can also register our own file types.

Or in the declarations panel in the Package.appxmanifest file, select the File type association and click Add:

Fill in the name and file type so that the system will call our app to process the file that encountered the. cnblogfile suffix. Of course, the system still has some reserved suffix file name, see https://msdn.microsoft.com/zh-CN/library/windows/apps/xaml/hh779669.

Of course we also need to get the corresponding file, this need to override the "onfileactivated" method in App.xaml.cs, the code is as follows:

protected override void onfileactivated (fileactivatedeventargs args)

{

foreach(StorageFile argfile in args. Files)

{

//process the corresponding file

}

}

This allows us to get a list of the files passed to us from the args properties, and then we can process them as we want. The Onfileactivated method should also add the code for the Start app section of the "OnLaunched" method.

The use of file calls can also be seen in the example https://code.msdn.microsoft.com/windowsapps/Association-Launching-535d2cec/mentioned above.

Not the same call

Both the Launchfileasync and Launchuriasync methods have overloads with Launcheroptions. See examples of use and instructions for https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.system.launcheroptions.aspx.

Share the code and change the world!

Windows Phone Store App Link:

http://www.windowsphone.com/zh-cn/store/app/ Blog Park -UAP/500F08F0-5BE8-4723-AFF9-A397BEEE52FC

Windows Store App Link:

http://apps.microsoft.com/windows/zh-cn/app/c76b99a0-9abd-4a4e-86f0-b29bfcc51059

GitHub Open Source Link:

Https://github.com/MS-UAP/cnblogs-UAP

MSDN Sample Code:

Https://code.msdn.microsoft.com/CNBlogs-Client-Universal-477943ab

Blog Park Client UAP Development Essay--building the bridge between apps

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.