Exemple #1
0
        private async void PinCoin_click(object sender, RoutedEventArgs e)
        {
            bool success;

            if (vm.Coin.IsPin)
            {
                success = await LiveTileUpdater.RemoveSecondaryTileAction(vm.Coin.Name);

                /// reset it even if it fails
                vm.Coin.IsPin = false;
                if (success)
                {
                    vm.InAppNotification($"Unpinned {vm.Coin.Name} from start screen");
                }
            }
            else
            {
                var grid = await LiveTileGenerator.SecondaryTileGridOperation(vm.Coin.Name);

                try {
                    RenderTargetBitmap rtb = new RenderTargetBitmap();
                    MainGrid.Children.Add(grid);
                    grid.Opacity = 0;
                    await rtb.RenderAsync(grid);

                    MainGrid.Children.Remove(grid);
                    var pixelBuffer = await rtb.GetPixelsAsync();

                    var pixels             = pixelBuffer.ToArray();
                    var displayInformation = DisplayInformation.GetForCurrentView();
                    var file = await ApplicationData.Current.LocalFolder.CreateFileAsync($"tile-{vm.Coin.Name}.png",
                                                                                         CreationCollisionOption.ReplaceExisting);

                    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Premultiplied,
                                             (uint)rtb.PixelWidth,
                                             (uint)rtb.PixelHeight,
                                             displayInformation.RawDpiX,
                                             displayInformation.RawDpiY,
                                             pixels);
                        await encoder.FlushAsync();
                    }
                }
                catch (Exception ex) {
                    var z = ex.Message;
                }

                success = await LiveTileUpdater.AddSecondaryTileAction(vm.Coin.Name);

                if (success)
                {
                    vm.Coin.IsPin = true;
                    vm.InAppNotification($"Pinned {vm.Coin.Name} to start screen");
                }
            }
        }
Exemple #2
0
        private async void PinUnpinCoin(object sender, RoutedEventArgs e)
        {
            var    card   = (HomeCard)((FrameworkElement)sender).DataContext;
            string crypto = card.Info.Name;

            var priceCard = vm.PriceCards.FirstOrDefault(c => c.Info.Name == crypto);

            if (priceCard == null)
            {
                return;
            }

            int  i       = vm.PriceCards.IndexOf(priceCard);
            bool success = false;

            if (card.Info.IsPin)
            {
                success = await LiveTileUpdater.RemoveSecondaryTileAction(crypto);

                vm.PriceCards[i].Info.IsPin = false;
                vm.InAppNotification($"Unpinned {crypto} from start screen.");
            }
            else
            {
                var grid = await LiveTileGenerator.SecondaryTileGridOperation(crypto);

                try {
                    RenderTargetBitmap rtb = new RenderTargetBitmap();
                    MainGrid.Children.Add(grid);
                    grid.Opacity = 0;
                    await rtb.RenderAsync(grid);

                    MainGrid.Children.Remove(grid);
                    var pixelBuffer = await rtb.GetPixelsAsync();

                    var pixels             = pixelBuffer.ToArray();
                    var displayInformation = DisplayInformation.GetForCurrentView();
                    var file = await ApplicationData.Current.LocalFolder.CreateFileAsync($"tile-{crypto}.png",
                                                                                         CreationCollisionOption.ReplaceExisting);

                    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Premultiplied,
                                             (uint)rtb.PixelWidth,
                                             (uint)rtb.PixelHeight,
                                             displayInformation.RawDpiX,
                                             displayInformation.RawDpiY,
                                             pixels);
                        await encoder.FlushAsync();
                    }
                }
                catch (Exception ex) {
                    var z = ex.Message;
                }


                success = await LiveTileUpdater.AddSecondaryTileAction(crypto);

                if (success)
                {
                    vm.PriceCards[i].Info.IsPin = true;
                    vm.InAppNotification($"Pinned {crypto} to start screen.");
                }
            }
        }