ExecuteServiceRequest() публичный Метод

public ExecuteServiceRequest ( ) : Task
Результат Task
Пример #1
0
        protected internal override async Task OnAction(ExecuteActionArgs e)
        {
            if (e.Action == "ExportToExcel" && !e.IsHandled)
            {
                await e.ExecuteServiceRequest();
                Exception error = null;
                try
                {
                    if (e.Result != null)
                    {
                        try
                        {
                            var data = e.Result["excelFile"].ValueDirect;
                            var parts = data.Split('|');
                            var bytes = Convert.FromBase64String(parts[1]);
                            var fileName = parts[0];

                            var folder = ApplicationData.Current.LocalFolder;
                            var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
                            using (var s = await file.OpenStreamForWriteAsync())
                                await s.WriteAsync(bytes, 0, bytes.Length);

                            await Launcher.LaunchFileAsync(file);
                        }
                        finally
                        {
                            e.Result = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    error = ex;
                }

                if (error != null)
                    await Service.Current.Hooks.ShowNotification(error.Message, NotificationType.Error);

                e.IsHandled = true;
            }
        }
Пример #2
0
        protected internal override async Task OnAction(ExecuteActionArgs e)
        {
            if (e.Action == "ExportToExcel" && !e.IsHandled)
            {
                await e.ExecuteServiceRequest();

                Exception error = null;
                try
                {
                    if (e.Result != null)
                    {
                        try
                        {
                            var savePicker = new FileSavePicker();
                            savePicker.SuggestedFileName = e.Query.Name + ".xlsx";
                            savePicker.FileTypeChoices.Add("Excel File", new List<string> { ".xlsx" });
                            savePicker.DefaultFileExtension = ".xlsx";

                            var fileToSave = await savePicker.PickSaveFileAsync();
                            if (fileToSave != null)
                            {
                                using (var stream = await fileToSave.OpenAsync(FileAccessMode.ReadWrite))
                                {
                                    using (var outputStream = stream.GetOutputStreamAt(0))
                                    {
                                        using (var dataWriter = new DataWriter(outputStream))
                                        {
                                            var data = e.Result["excelFile"].ValueDirect;
                                            data = data.Substring(data.IndexOf('|') + 1);
                                            dataWriter.WriteBytes(Convert.FromBase64String(data));
                                            await dataWriter.StoreAsync();
                                            dataWriter.DetachStream();
                                        }
                                    }
                                    await stream.FlushAsync();
                                }
                                await Windows.System.Launcher.LaunchFileAsync(fileToSave);
                            }
                        }
                        finally
                        {
                            e.Result = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    error = ex;
                }

                if (error != null)
                {
                    var dialog = new MessageDialog(Service.Current.Messages["CouldNotOpenExcelFile"], string.Empty);
                    dialog.Commands.Add(new UICommand(Service.Current.Messages["OK"]));
                    await dialog.ShowAsync();
                }

                e.IsHandled = true;
            }
        }