设为首页 收藏本站
查看: 1159|回复: 0

windows phone 8 中的应用间通信

[复制链接]

尚未签到

发表于 2015-5-23 06:25:11 | 显示全部楼层 |阅读模式
  windows phone 8 中应用间的通信,之前在windows phone 7 在一部手机中的应用间想进行一些数据通信除了使用service, 在应用间几乎是不可能,但是在windows phone 8中SDK给了我们这样的API今天就为大家详细介绍下。
  此文是 升级到WP8必需知道的13个特性 系列的一个更新 希望这个系列可以给 Windows Phone 8开发者带来一些开发上的便利。
  同时欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick
  1. 文件关联应用
  作为一个接收共享文件的应用 是将一个文件保存在共享隔离存储器中然后由目标应用从共享隔离存储器中取出文件的过程。
  首先介绍下如何注册成为一个可以接收文件的应用
  注册您的应用为一个支持某种文件类型的应用,一旦你的应用安装到用户的机器上后用户尝试打开此种类型文件在选择列表中就会出现你的应用图标。
  应用图标尺寸如下:
DSC0000.png
  并且需要在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之间的数字小写字母 英文句点(.)和连字符(-)






DSC0001.png
  如何监听一个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
DSC0002.png
  应用的关联可以是


  • 邮件的附件
  • IE浏览器中的一个文件
  • 或者是一个NFC的Tag标签
  • 一个应用程序的共享文件
  系统保留的文件类型和scheme name 列表
  请参考:
  http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207065(v=vs.105).aspx
DSC0003.png
  
  今天就给大家介绍到这里,说的不明白的欢迎大家拍砖 也可以在新浪微博上 @王博_Nick

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69646-1-1.html 上篇帖子: Windows 8消费者预览版发布啦(附离线分享) 下篇帖子: 重新想象 Windows 8 Store Apps (4)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表