Windows Phone 8.1 应用间共享_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Windows Phone 8.1 应用间共享

Windows Phone 8.1 应用间共享

 2014/5/29 15:46:06  消失3003  博客园  我要评论(0)
  • 摘要:(1)LaunchUriAsync将简单数据包含在Uri中,然后发送到目标应用:awaitLauncher.LaunchUriAsync(newUri("target:message"));(2)LaunchFileAsync直接用默认应用打开某文件:StorageFilefile=awaitStorageFile.GetFileFromApplicationUriAsync(newUri("ms-appx:///Text.txt"));awaitLauncher
  • 标签:Windows 应用

(1)LaunchUriAsync

将简单数据包含在 Uri 中,然后发送到目标应用:

await Launcher.LaunchUriAsync(new Uri("target:message"));

 

(2)LaunchFileAsync

直接用默认应用打开某文件:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Text.txt"));
await Launcher.LaunchFileAsync(file);

 

(3)App To App

也就是将 Source应用 的内容共享到 Target应用 当中去,共享的过程为:Soucre -> Shell -> Target -> Source,其中 Shell 为系统的选择 Target应用 的界面。

A. Source:

1)注册 DataRequested 事件并设置需要共享的 Data:

private void shareTextButton_Click(object sender, RoutedEventArgs e)
{
    DataTransferManager.GetForCurrentView().DataRequested += ShareText_DataRequested;
    DataTransferManager.ShowShareUI();
}

void ShareText_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    DataRequest request = args.Request;
    request.Data.Properties.Title = "Text";
    request.Data.Properties.Description = "You MUST set a Title! ";
    request.Data.SetText("Some Texts. ");
}

需要注意的是必须设置 Data 的 Title 属性,不然会共享失败。

当然也可以共享文件:

private void shareButton_Click(object sender, RoutedEventArgs e)
{
    DataTransferManager.GetForCurrentView().DataRequested += ShareTextAndImage_DataRequested;
    DataTransferManager.ShowShareUI();
}

private async void ShareTextAndImage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    var deferral = args.Request.GetDeferral();

var request = args.Request;
    request.Data.Properties.Title = "Text and Image";
    request.Data.Properties.Description = "You MUST set a Title! Async should get Deferral. ";

    request.Data.SetText(myTextBox.Text.Trim());

    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/SmallLogo.scale-240.png"));
    request.Data.SetStorageItems(new List<StorageFile>() { file });

    deferral.Complete();
}

当事件中有异步方法是需要获取 Deferral,以保证异步事件的成功执行。

2)在离开页面时取消事件的注册

private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
    DataTransferManager.GetForCurrentView().DataRequested -= ShareText_DataRequested;
    DataTransferManager.GetForCurrentView().DataRequested -= ShareTextAndImage_DataRequested;
}


B. Target

1)在 Manifest 中添加共享目标

只有当有符合共享目标的数据格式或文件类型的共享内容时,Shell 的列表中才会出现该 Target应用。

2)App.xaml.cs 中重写 OnShareTargetActivated

protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;

    if( rootFrame == null )
    {
        rootFrame = new Frame();
        Window.Current.Content = rootFrame;
    }

    if( rootFrame.Content == null )
    {
        rootFrame.Navigate(typeof(MainPage), args.ShareOperation);
    }

    Window.Current.Activate();
}

3)在 Page 的 OnNavigatedTo 方法中获取共享内容

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    shareContent = e.Parameter as ShareOperation;

    myTextBlock.Text = await shareContent.Data.GetTextAsync();

    var files = await shareContent.Data.GetStorageItemsAsync();
    if( files != null && files.Count > 0 )
    {
        BitmapImage image = new BitmapImage();
        await image.SetSourceAsync(await (files[0] as StorageFile).OpenAsync(FileAccessMode.Read));
        myImage.Source = image;
    }
}

ShareOperation shareContent;

4)通过 ReportCompleted 方法回到 Source应用

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
    if( shareContent != null )
    {
        shareContent.ReportCompleted();
    }
}

 

上一篇: 贴膜不是终点 苏宁请你免费喝酸奶 下一篇: 没有下一篇了!
发表评论
用户名: 匿名