From 85389c033a61f0b0e922646e8f24d405fec1a3b1 Mon Sep 17 00:00:00 2001 From: Jim O'Neil Date: Thu, 20 Jun 2013 00:50:47 -0400 Subject: [PATCH] implemented sharing contract --- .../LeftPanel.xaml.cs | 56 ++++++++++++++++++- .../MainPage.xaml.cs | 12 ++++ .../APIMASH_APIs/APIMASH_TomTom.cs | 8 ++- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/LeftPanel.xaml.cs b/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/LeftPanel.xaml.cs index 8c91836..25d6f68 100644 --- a/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/LeftPanel.xaml.cs +++ b/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/LeftPanel.xaml.cs @@ -4,11 +4,17 @@ using APIMASH_StarterKit.Mapping; using Bing.Maps; using System; +using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Threading.Tasks; +using Windows.ApplicationModel.DataTransfer; +using Windows.Storage; +using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using System.IO; +using System.Runtime.InteropServices.WindowsRuntime; // // LICENSE: http://aka.ms/LicenseTerms-SampleApps @@ -110,7 +116,55 @@ public LeftPanel() this.DefaultViewModel["ApiViewModel"] = _tomTomApi.TomTomViewModel; _tomTomApi.TomTomViewModel.Results.CollectionChanged += Results_CollectionChanged; } - + + // + // TODO: implement code needed to share item from main page + // +public async void GetSharedData(DataTransferManager sender, DataRequestedEventArgs args) +{ + try + { + var currentCam = MappableListView.SelectedItem as APIMASH_TomTom.TomTomCameraViewModel; + if (currentCam != null) + { + + DataRequestDeferral deferral = args.Request.GetDeferral(); + + args.Request.Data.Properties.Title = String.Format("TomTom Camera: {0}", currentCam.CameraId); + args.Request.Data.Properties.Description = currentCam.Name; + + // share a file + var file = await StorageFile.CreateStreamedFileAsync( + String.Format("{0}_{1}.jpg", currentCam.CameraId, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")), + async stream => + { + await stream.WriteAsync(currentCam.ImageBytes.AsBuffer()); + await stream.FlushAsync(); + stream.Dispose(); + }, + null); + args.Request.Data.SetStorageItems(new List { file }); + + // share as bitmap + InMemoryRandomAccessStream raStream = new InMemoryRandomAccessStream(); + await raStream.WriteAsync(currentCam.ImageBytes.AsBuffer()); + await raStream.FlushAsync(); + args.Request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(raStream)); + + deferral.Complete(); + } + else + { + args.Request.FailWithDisplayText("Select a camera to share its image."); + } + } + catch (Exception ex) + { + args.Request.FailWithDisplayText(ex.Message); + } +} + + /// /// Carries out application-specific handling of the item selected in the listview. The synchronization with /// the map display is already accomodated. diff --git a/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/MainPage.xaml.cs b/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/MainPage.xaml.cs index 1a30454..55fe61a 100644 --- a/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/MainPage.xaml.cs +++ b/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH-TomTom_BingMaps-StarterKit/MainPage.xaml.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Runtime.Serialization; using System.Threading.Tasks; +using Windows.ApplicationModel.DataTransfer; using Windows.ApplicationModel.Search; using Windows.Devices.Geolocation; using Windows.Storage; @@ -250,6 +251,17 @@ protected override void OnNavigatedTo(NavigationEventArgs e) // reset map to last known view TheMap.SetView(new Location(_pageState.MapCenter.Latitude, _pageState.MapCenter.Longitude), _pageState.Zoom, MapAnimationDuration.None); } + + DataTransferManager dtm = DataTransferManager.GetForCurrentView(); + dtm.DataRequested += LeftPanel.GetSharedData; + } + + protected override void OnNavigatedFrom(NavigationEventArgs e) + { + base.OnNavigatedFrom(e); + + DataTransferManager dtm = DataTransferManager.GetForCurrentView(); + dtm.DataRequested -= LeftPanel.GetSharedData; } /// diff --git a/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH_APIs/APIMASH_TomTom.cs b/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH_APIs/APIMASH_TomTom.cs index b27590a..446b917 100644 --- a/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH_APIs/APIMASH_TomTom.cs +++ b/Windows Starter Kits/APIMASH_TomTom_BingMaps_StarterKit/APIMASH_APIs/APIMASH_TomTom.cs @@ -34,6 +34,9 @@ public class TomTomCameraViewModel : BindableBase, IMappable public String Orientation { get; set; } public Int32 RefreshRate { get; set; } + private Byte[] _imageBytes; + public Byte[] ImageBytes { get; set; } + private BitmapImage _image; public BitmapImage Image { @@ -130,9 +133,10 @@ public static Boolean PopulateViewModel(cameras model, ObservableCollection GetCameraImage(TomTomCameraViewMode } // populate the ViewModel with the image - TomTomCamerasModel.PopulateViewModel(cameraImage, camera); + TomTomCamerasModel.PopulateViewModel(cameraImage, apiResponse.RawResponse, camera); // return a success status (there will always be an image returned) return ApiResponseStatus.Default;