Beispiel #1
0
        public async void ExportToClipboard()
        {
            try
            {
                TelemetryExtension.Current?.TrackEvent("Action_ExportSchedule_ViaClipboard");
                IsEnabled = false;

                var randomAccessStream = await RenderTargetBitmapHelper.ExportToStream(Element, Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId);

                try
                {
                    // Also write into a storage file
                    var tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Schedule.jpg", CreationCollisionOption.ReplaceExisting);

                    using (var stream = await tempFile.OpenStreamForWriteAsync())
                    {
                        await randomAccessStream.AsStreamForRead().CopyToAsync(stream);
                    }

                    var streamReference = RandomAccessStreamReference.CreateFromStream(randomAccessStream);

                    DataPackage dp = new DataPackage();
                    dp.SetBitmap(streamReference);
                    dp.SetStorageItems(new IStorageItem[] { tempFile });

                    dp.Destroyed += delegate
                    {
                        try
                        {
                            randomAccessStream.Dispose();
                        }
                        catch { }
                    };

                    Clipboard.SetContent(dp);
                }
                catch
                {
                    randomAccessStream.Dispose();
                    throw;
                }

                GoBack();
            }

            catch (Exception ex)
            {
                TelemetryExtension.Current?.TrackException(ex);
                IsEnabled = true;
            }
        }
Beispiel #2
0
        public async void ExportToFile()
        {
            try
            {
                TelemetryExtension.Current?.TrackEvent("Action_ExportSchedule_ViaFile");
                IsEnabled = false;

                FileSavePicker picker = new FileSavePicker
                {
                    DefaultFileExtension = ".jpg"
                };
                picker.FileTypeChoices.Add(new KeyValuePair <string, IList <string> >("JPEG", new List <string>()
                {
                    ".jpg"
                }));
                picker.SuggestedFileName      = "Schedule";
                picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                var storageFile = await picker.PickSaveFileAsync();

                if (storageFile != null)
                {
                    using (var randomAccessStream = await RenderTargetBitmapHelper.ExportToStream(Element, Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId))
                    {
                        using (var stream = await storageFile.OpenStreamForWriteAsync())
                        {
                            await randomAccessStream.AsStreamForRead().CopyToAsync(stream);
                        }
                    }

                    GoBack();
                }
                else
                {
                    IsEnabled = true;
                }
            }

            catch (Exception ex)
            {
                TelemetryExtension.Current?.TrackException(ex);
                IsEnabled = true;
            }
        }
Beispiel #3
0
        private async void ShareDataRequested(DataRequestedEventArgs args)
        {
            var request = args.Request;

            var deferral = request.GetDeferral();

            try
            {
                var randomAccessStream = await RenderTargetBitmapHelper.ExportToStream(Element, Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId);

                try
                {
                    // Also write into a storage file
                    var tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Schedule.jpg", CreationCollisionOption.ReplaceExisting);

                    using (var stream = await tempFile.OpenStreamForWriteAsync())
                    {
                        await randomAccessStream.AsStreamForRead().CopyToAsync(stream);
                    }

                    // Provide both bitmap and storage item, since some apps only support one
                    request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(randomAccessStream));
                    request.Data.SetStorageItems(new IStorageItem[] { tempFile });

                    request.Data.RequestedOperation = DataPackageOperation.Copy;
                    request.Data.Properties.Title   = "Schedule";

                    request.Data.Destroyed += delegate
                    {
                        try
                        {
                            randomAccessStream.Dispose();
                        }
                        catch { }
                    };

                    request.Data.OperationCompleted += delegate
                    {
                        try
                        {
                            if (IsCurrentNavigatedPage)
                            {
                                GoBack();
                            }
                        }
                        catch { }
                    };
                }

                catch (Exception ex)
                {
                    randomAccessStream.Dispose();
                    throw ex;
                }
            }

            catch (Exception ex)
            {
                TelemetryExtension.Current?.TrackException(ex);
            }

            finally
            {
                IsEnabled = true;
                deferral.Complete();
            }
        }