private async void Initialize(object sender, RoutedEventArgs e, bool deleteTile) { if (handlingClick) { return; } this.viewModel.StatusMessage = "Running ..."; handlingClick = true; try { IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync(); bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]); if (pairedBands.Length < 1) { this.viewModel.StatusMessage = "This sample app requires a Microsoft Band paired to your device. Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app."; return; } BandTile myTile = new BandTile(myTileId) { Name = "My Tile", TileIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconLarge.png"), SmallIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconSmall.png") }; await BuildLayout(myTile); this.viewModel.StatusMessage = "Woo got the layout ..."; // Remove the Tile from the Band, if present. An application won't need to do this everytime it runs. // But in case you modify this sample code and run it again, let's make sure to start fresh. if (deleteTile) { await bandClient.TileManager.RemoveTileAsync(myTileId); } if (!bandClient.TileManager.TileInstalledAndOwned(ref myTileId, new System.Threading.CancellationToken())) { // Create the Tile on the Band. await bandClient.TileManager.AddTileAsync(myTile); } Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { TileUIUpdater t = new TileUIUpdater(bandClient, myTileId); t.resetPages(); }); this.viewModel.StatusMessage = "Woo reset the pages ..."; // Subscribe to Tile events. TaskCompletionSource<bool> closePressed = new TaskCompletionSource<bool>(); bandClient.TileManager.TileButtonPressed += EventHandler_TileButtonPressed; bandClient.TileManager.TileClosed += (s, args) => { closePressed.TrySetResult(true); }; this.viewModel.StatusMessage = "Woo added the event handlers ..."; try { await bandClient.TileManager.StartReadingsAsync(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } /* // Receive events until the Tile is closed. this.viewModel.StatusMessage = "Check the Tile on your Band (it's the last Tile). Waiting for events ..."; await closePressed.Task; // Stop listening for Tile events. await bandClient.TileManager.StopReadingsAsync(); this.viewModel.StatusMessage = "Done.";*/ } catch (Exception ex) { this.viewModel.StatusMessage = ex.ToString(); } finally { handlingClick = false; } }
async void EventHandler_TileButtonPressed(object sender, BandTileEventArgs<IBandTileButtonPressedEvent> e) { // This method is called when the user presses the // button in our tile’s layout. // // e.TileEvent.TileId is the tile’s Guid. // e.TileEvent.Timestamp is the DateTimeOffset of the event. // e.TileEvent.PageId is the Guid of our page with the button. // e.TileEvent.ElementId is the value assigned to the button Guid pageId = e.TileEvent.PageId; StorageFolder folder = ApplicationData.Current.LocalFolder; if (Helpers.containsKeyDictionary(pageId, folder)) { string switchName = Helpers.getValueByKeyDictionary(pageId, folder); string value; if (e.TileEvent.ElementId == 3) { value = "on"; } else { value = "off"; } Helpers.apiCall("set/" + Uri.EscapeUriString(switchName) + "/" + value); string apiCallResult = Helpers.apiCall("switches"); //IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync(); //bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]); TileUIUpdater t = new TileUIUpdater(bandClient, myTileId); t.updatePages(JArray.Parse(apiCallResult)); } #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed }