windows phone 8 中的应用间通信
windows phone 8 中应用间的通信,之前在windows phone 7 在一部手机中的应用间想进行一些数据通信除了使用service, 在应用间几乎是不可能,但是在windows phone 8中SDK给了我们这样的API今天就为大家详细介绍下。此文是 升级到WP8必需知道的13个特性 系列的一个更新 希望这个系列可以给 Windows Phone 8开发者带来一些开发上的便利。
同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick
1. 文件关联应用
作为一个接收共享文件的应用 是将一个文件保存在共享隔离存储器中然后由目标应用从共享隔离存储器中取出文件的过程。
首先介绍下如何注册成为一个可以接收文件的应用
注册您的应用为一个支持某种文件类型的应用,一旦你的应用安装到用户的机器上后用户尝试打开此种类型文件在选择列表中就会出现你的应用图标。
应用图标尺寸如下:
并且需要在Manifest文件中指定支持的文件类型:
Assets/sdk-small-33x33.png
Assets/sdk-medium-69x69.png
Assets/sdk-large-176x176.png
.sdkTest1
.sdkTest2
Logo中指定在不同情况下显示的图标
FileType中注册的是支持文件类型 这里最多支持20个不同的文件类型
监听一个文件的操作:
实际上当你的应用受到一个打开文件的请求时 应用程序是接收到一个包含 获取在共享隔离存储器中的一个Token连接的:
/FileTypeAssociation?fileToken=89819279-4fe0-4531-9f57-d633f0949a19
所以在我们的TargetApp中需要处理下接收参数 方法如下使用App中的InitializePhoneApplication方法
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Assign the URI-mapper class to the application frame.
RootFrame.UriMapper = new AssociationUriMapper();
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
AssociationUriMapper 的实现
using System;
using System.IO;
using System.Windows.Navigation;
using Windows.Phone.Storage.SharedAccess;
namespace sdkAutoLaunch
{
class AssociationUriMapper : UriMapperBase
{
private string tempUri;
public override Uri MapUri(Uri uri)
{
tempUri = uri.ToString();
// File association launch
if (tempUri.Contains("/FileTypeAssociation"))
{
// Get the file ID (after "fileToken=").
int fileIDIndex = tempUri.IndexOf("fileToken=") + 10;
string fileID = tempUri.Substring(fileIDIndex);
// Get the file name.
string incomingFileName =
SharedStorageAccessManager.GetSharedFileName(fileID);
// Get the file extension.
string incomingFileType = Path.GetExtension(incomingFileName);
// Map the .sdkTest1 and .sdkTest2 files to different pages.
switch (incomingFileType)
{
case ".sdkTest1":
return new Uri("/sdkTest1Page.xaml?fileToken=" + fileID, UriKind.Relative);
case ".sdkTest2":
return new Uri("/sdkTest2Page.xaml?fileToken=" + fileID, UriKind.Relative);
default:
return new Uri("/MainPage.xaml", UriKind.Relative);
}
}
// Otherwise perform normal launch.
return uri;
}
}
}
导航页面中获取参数的方法
// Get a dictionary of URI parameters and values.
IDictionary queryStrings = this.NavigationContext.QueryString;
当你获取到共享文件的Token后你就可以从共享存储空间通过 GetSharedFileName文件名称(包括文件在拓展名)和 CopySharedFileAsync 将共享文件拷贝到Target应用的隔离存储区
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
IDictionary queryStrings = this.NavigationContext.QueryString;
string fileToken = queryStrings["fileToken"];
var filename = SharedStorageAccessManager.GetSharedFileName(fileToken);
var file = await SharedStorageAccessManager.CopySharedFileAsync(Windows.Storage.ApplicationData.Current.LocalFolder,
filename, Windows.Storage.NameCollisionOption.ReplaceExisting,
fileToken);
StreamResourceInfo reader = Application.GetResourceStream(new Uri(file.Path, UriKind.Relative));
StreamReader streamRead = new StreamReader(reader.Stream);
string responseString = streamRead.ReadToEnd();
streamRead.Close();
streamRead.Dispose();
MessageBox.Show(responseString);
}
作为一个发出共享文件的应用要做的相对简单许多使用Windows.System.Launcher.LaunchFileAsync 即可
private async void LaunchFileButton_Click(object sender, RoutedEventArgs rea)
{
// Access isolated storage.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
// Access the bug query file.
StorageFile bqfile = await local.GetFileAsync("file1.bqy");
// Launch the bug query file.
Windows.System.Launcher.LaunchFileAsync(bqfile);
}
2. URl关联应用 简单的说就是使用一个类似 URL的string 字符串来启动某个应用程序 (传参数形式和传统的网页十分相似)
首先介绍如何注册成为一个支持 URI assocations 的一个应用
每个应用可以声明为一个契约 Protocol Name 这个契约名称是启动这个 target app 的关键字也是要在Manifest文件中声明
Name的string 长度要求在2-18个char之间的数字小写字母 英文句点(.)和连字符(-)
如何监听一个URl并且获得参数
同样在target App中的InitializePhoneApplication 使用 AssociationUriMapper来实现判断
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Assign the URI-mapper class to the application frame.
RootFrame.UriMapper = new AssociationUriMapper();
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
AssociationUriMapper 和 文件的稍有不同
using System;
using System.Windows.Navigation;
namespace sdkAutoLaunch
{
class AssociationUriMapper : UriMapperBase
{
private string tempUri;
public override Uri MapUri(Uri uri)
{
tempUri = System.Net.HttpUtility.UrlDecode(uri.ToString());
// URI association launch for contoso.
if (tempUri.Contains("contoso:ShowProducts?CategoryID="))
{
// Get the category ID (after "CategoryID=").
int categoryIdIndex = tempUri.IndexOf("CategoryID=") + 11;
string categoryId = tempUri.Substring(categoryIdIndex);
// Map the show products request to ShowProducts.xaml
return new Uri("/ShowProducts.xaml?CategoryID=" + categoryId, UriKind.Relative);
}
// Otherwise perform normal launch.
return uri;
}
}
}
同样在Target app 的 target page的OnNavigatedTo事件中获取参数
// Get a dictionary of URI parameters and values.
IDictionary queryStrings = this.NavigationContext.QueryString;
在source App中调用 target App相对来说非常简单
private async void LaunchContosoNewProductsButton_Click(object sender, RoutedEventArgs rea)
{
// Launch URI.
Windows.System.Launcher.LaunchUriAsync(new System.Uri("contoso:NewProducts"));
}
这里要特别强调的一点是 目标应用受到的信息是
“/Protocol?encodedLaunchUri=contoso%3ANewProducts”. 这样的一段URI 需要你的AssociationUriMapper帮忙decode处理一下
URL的Launching同样支持使用与基于NFC的近场通讯技术
ProximityDevice device = ProximityDevice.GetDefault();
// Make sure NFC is supported
if (device != null)
{
long Id = device.PublishUriMessage(new System.Uri("contoso:NewProducts"));
Debug.WriteLine("Published Message. ID is {0}", Id);
// Store the unique message Id so that it
// can be used to stop publishing this message
}
LaunchUriAsync 不仅仅可以Launch其他应用 同样可以唤起URL Email 和各种Setting 以及 WindowsPhone应用商店中的内容
详细内容 :http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662937(v=vs.105).aspx
应用的关联可以是
[*]邮件的附件
[*]IE浏览器中的一个文件
[*]或者是一个NFC的Tag标签
[*]一个应用程序的共享文件
系统保留的文件类型和scheme name 列表
请参考:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207065(v=vs.105).aspx
今天就给大家介绍到这里,说的不明白的欢迎大家拍砖 也可以在新浪微博上 @王博_Nick
		页: 
[1]