windows phone 8 中應用間的通信,之前在windows phone 7 在一部手機中的應用間想進行一些數據通信除了使用service, 在應用間幾乎是不可能,但是在windows phone 8中SDK給了我們這樣的API今天就為大家詳細介紹下。
此文是 升級到WP8必需知道的13個特性 系列的一個更新 希望這個系列可以給 Windows Phone 8開發者帶來一些開發上的便利。
考,這篇文章只先作為一個功能目錄後面我會逐一詳細的介紹各項功能。
升級到WP8必需知道的13個特性 系列文章目錄地址:http://www.linuxidc.com/Linux/2013-08/89003.htm
1. 文件關聯應用
作為一個接收共享文件的應用 是將一個文件保存在共享隔離存儲器中然後由目標應用從共享隔離存儲器中取出文件的過程。
首先介紹下如何注冊成為一個可以接收文件的應用
注冊您的應用為一個支持某種文件類型的應用,一旦你的應用安裝到用戶的機器上後用戶嘗試打開此種類型文件在選擇列表中就會出現你的應用圖標。
應用圖標尺寸如下:
並且需要在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);
}