diff --git a/ClassevivaPCTO/Dialogs/NoticeDialogContent.xaml.cs b/ClassevivaPCTO/Dialogs/NoticeDialogContent.xaml.cs index d2cb9e79..dd007b0b 100644 --- a/ClassevivaPCTO/Dialogs/NoticeDialogContent.xaml.cs +++ b/ClassevivaPCTO/Dialogs/NoticeDialogContent.xaml.cs @@ -41,7 +41,7 @@ public NoticeDialogContent(Notice notice, NoticeReadResult noticeReadResult) AttachmentsListView.ItemsSource = notice.attachments; - App app = (App)App.Current; + App app = (App) App.Current; var apiClient = app.Container.GetService(); apiWrapper = PoliciesDispatchProxy.CreateProxy(apiClient); @@ -50,16 +50,25 @@ public NoticeDialogContent(Notice notice, NoticeReadResult noticeReadResult) private async void ButtonOpen_Click(object sender, RoutedEventArgs e) { var senderbutton = sender as AppBarButton; - var currentAttachment = (NoticeAttachment)senderbutton.DataContext; + var currentAttachment = (NoticeAttachment) senderbutton.DataContext; - byte[] bytes = await GetAttachmentAsBytes(currentAttachment); + await Task.Run(async () => + { + byte[] bytes = await GetAttachmentAsBytes(currentAttachment); - var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync( - currentAttachment.fileName, - Windows.Storage.CreationCollisionOption.ReplaceExisting - ); - await Windows.Storage.FileIO.WriteBytesAsync(file, bytes); - await Windows.System.Launcher.LaunchFileAsync(file); + + //run on ui thread + await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => + { + var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync( + currentAttachment.fileName, + Windows.Storage.CreationCollisionOption.ReplaceExisting + ); + await Windows.Storage.FileIO.WriteBytesAsync(file, bytes); + + var success = await Windows.System.Launcher.LaunchFileAsync(file); + }); + }); } private async void ButtonSave_Click(object sender, RoutedEventArgs e) @@ -68,39 +77,46 @@ private async void ButtonSave_Click(object sender, RoutedEventArgs e) var currentAttachment = senderbutton.DataContext as NoticeAttachment; - byte[] bytes = await GetAttachmentAsBytes(currentAttachment); - - var savePicker = new Windows.Storage.Pickers.FileSavePicker(); - savePicker.SuggestedStartLocation = - Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; - - savePicker.FileTypeChoices.Add("Allegato", new List() { "." }); - savePicker.SuggestedFileName = currentAttachment.fileName; - - Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync(); - if (file != null) + await Task.Run(async () => { - // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync. - Windows.Storage.CachedFileManager.DeferUpdates(file); - - //scrivo il file - await Windows.Storage.FileIO.WriteBytesAsync(file, bytes); + byte[] bytes = await GetAttachmentAsBytes(currentAttachment); - Windows.Storage.Provider.FileUpdateStatus status = - await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); - if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) + //run on ui thread + await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { - //completato - } - else - { - //trakkar errore? - } - } - else - { - //we need to track the error - } + var savePicker = new Windows.Storage.Pickers.FileSavePicker(); + savePicker.SuggestedStartLocation = + Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; + + savePicker.FileTypeChoices.Add("Allegato", new List() {"."}); + savePicker.SuggestedFileName = currentAttachment.fileName; + + Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync(); + if (file != null) + { + // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync. + Windows.Storage.CachedFileManager.DeferUpdates(file); + + //scrivo il file + await Windows.Storage.FileIO.WriteBytesAsync(file, bytes); + + Windows.Storage.Provider.FileUpdateStatus status = + await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); + if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) + { + //completato + } + else + { + //trakkar errore? + } + } + else + { + //we need to track the error + } + }); + }); } @@ -114,6 +130,7 @@ private async Task GetAttachmentAsBytes(NoticeAttachment attachment) CurrentNotice.evtCode, attachment.attachNum.ToString() ); + byte[] bytes = await attachmentBinary.Content.ReadAsByteArrayAsync(); return bytes;