private async void Grayscale_Click(object sender, RoutedEventArgs e) { using (var connection = new AppServiceConnection()) { connection.AppServiceName = "com.microsoft.grayscaleservice"; connection.PackageFamilyName = "AppExtensibility.Extension.Grayscale_byq669axdz8jy"; AppServiceConnectionStatus status = await connection.OpenAsync(); if (status == AppServiceConnectionStatus.Success) { #region SendMessage // send request to service var request = new ValueSet(); request.Add("Command", "Grayscale"); request.Add("Pixels", ImageTools.GetBitmapBytes(AppData.currentImage)); request.Add("Height", AppData.currentImage.PixelHeight); request.Add("Width", AppData.currentImage.PixelWidth); // get response AppServiceResponse response = await connection.SendMessageAsync(request); if (response.Status == AppServiceResponseStatus.Success) #endregion #region HandleMessage { #region ErrorHandling // convert imagestring back ValueSet message = response.Message as ValueSet; if (message.ContainsKey("Pixels") && message.ContainsKey("Height") && message.ContainsKey("Width")) { #endregion byte[] pixels = message["Pixels"] as byte[]; int height = (int)message["Height"]; int width = (int)message["Width"]; // encode the bytes to a string, and then the image. string encodedImage = await ImageTools.EncodeBytesToPNGString(pixels, (uint)width, (uint)height); await AppData.currentImage.SetSourceAsync(ImageTools.DecodeStringToBitmapSource(encodedImage)); AppData.currentImageString = encodedImage; } } #endregion } } }
// this calls the 'extensionLoad' function in the script file, if it exists. public async void InvokeLoad(string str) { if (this._loaded) { if (_serviceName == null) { try { // this is dangerous! await _extwebview.InvokeScriptAsync("extensionLoad", new string[] { str }); } catch (Exception e) { // show errors return; } } #region App Service // App services are a better approach! else { try { // do app service call using (var connection = new AppServiceConnection()) { // service name was in properties connection.AppServiceName = _serviceName; // package Family Name is in the extension connection.PackageFamilyName = _extension.Package.Id.FamilyName; // open connection AppServiceConnectionStatus status = await connection.OpenAsync(); if (status != AppServiceConnectionStatus.Success) { Debug.WriteLine("Failed App Service Connection"); } else { // send request to service var request = new ValueSet(); request.Add("Command", "Load"); request.Add("Pixels", ImageTools.GetBitmapBytes(AppData.currentImage)); request.Add("Height", AppData.currentImage.PixelHeight); request.Add("Width", AppData.currentImage.PixelWidth); // get response AppServiceResponse response = await connection.SendMessageAsync(request); if (response.Status == AppServiceResponseStatus.Success) { ValueSet message = response.Message as ValueSet; if (message.ContainsKey("Pixels") && message.ContainsKey("Height") && message.ContainsKey("Width")) { byte[] pixels = message["Pixels"] as byte[]; int height = (int)message["Height"]; int width = (int)message["Width"]; #region Set Image to the new pixels // encode the bytes to a string, and then the image // this is for interop with the js extensions // wouldn't be needed if all extensions were implemented as app services string encodedImage = await ImageTools.EncodeBytesToPNGString(pixels, (uint)width, (uint)height); await AppData.currentImage.SetSourceAsync(ImageTools.DecodeStringToBitmapSource(encodedImage)); AppData.currentImageString = encodedImage; #endregion } } } } } catch (Exception e) { MessageDialog md = new MessageDialog("Invoking app service failed!"); await md.ShowAsync(); } } #endregion } }