Windows Phone 8 中的应用间通信


windows phone 8 中应用间的通信,之前在windows phone 7 在一部手机中的应用间想进行一些数据通信除了使用service, 在应用间几乎是不可能,但是在windows phone 8中SDK给了我们这样的API今天就为大家详细介绍下。

此文是 升级到WP8必需知道的13个特性 系列的一个更新 希望这个系列可以给 Windows Phone 8开发者带来一些开发上的便利。

考,这篇文章只先作为一个功能目录后面我会逐一详细的介绍各项功能。

升级到WP8必需知道的13个特性 系列文章目录地址:

1. 文件关联应用

作为一个接收共享文件的应用 是将一个文件保存在共享隔离存储器中然后由目标应用从共享隔离存储器中取出文件的过程。

首先介绍下如何注册成为一个可以接收文件的应用

注册您的应用为一个支持某种文件类型的应用,一旦你的应用安装到用户的机器上后用户尝试打开此种类型文件在选择列表中就会出现你的应用图标。

应用图标尺寸如下:

image

并且需要在Manifest文件中指定支持的文件类型:

 
<Extensions> 
   <FileTypeAssociation Name="Windows Phone SDK test file type" TaskID="_default" NavUriFragment="fileToken=%s"> 
       <Logos> 
           <Logo Size="small" IsRelative="true">Assets/sdk-small-33x33.png</Logo> 
           <Logo Size="medium" IsRelative="true">Assets/sdk-medium-69x69.png</Logo> 
           <Logo Size="large" IsRelative="true">Assets/sdk-large-176x176.png</Logo> 
       </Logos> 
       <SupportedFileTypes> 
         <FileType ContentType="application/sdk">.sdkTest1</FileType> 
         <FileType ContentType="application/sdk">.sdkTest2</FileType>

       </SupportedFileTypes> 
   </FileTypeAssociation> 
</Extensions>
 

 

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<string, string> queryStrings = this.NavigationContext.QueryString;
 

当你获取到共享文件的Token后你就可以从共享存储空间通过 GetSharedFileName文件名称(包括文件在拓展名)和 CopySharedFileAsync 将共享文件拷贝到Target应用的隔离存储区

 
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            IDictionary<string, string> 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);

}
  • 1
  • 2
  • 下一页

相关内容