Answered by:
Sharing charm attach a file

Question
-
i have an application that should provide sharing possibility, so here is my code:
DataRequestDeferral deferral = e.Request.GetDeferral(); var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Utils\shareappemail.html"); var file2 = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("SCLogFile.txt"); var stream = await file.OpenReadAsync(); var rdr = new StreamReader(stream.AsStream()); var contents = await rdr.ReadToEndAsync(); DataPackage requestData = e.Request.Data; requestData.Properties.Title = "Bla"; requestData.Properties.Description = "BlablaBla"; // The description is optional. requestData.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(contents)); List<IStorageItem> imageItems = new List<IStorageItem>(); imageItems.Add(file2); requestData.SetStorageItems(imageItems); deferral.Complete();
On my developer PC this thing works and file is being attached. But on my tablet it doesn't attach the file - why?
My two tablets run - Windows 8 Pro and Windows 8 Enterprise - both don't attach files. Both PC's running Windows 8 Pro - attach.
Foma
- Edited by Cheese1991 Friday, January 3, 2014 8:51 AM
Friday, January 3, 2014 8:50 AM
Answers
-
you need to set the filetypes property when using a dataprovider.
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e) { e.Request.Data.Properties.Title = "THIS IS A TITLE"; e.Request.Data.Properties.Description = "THIS IS A DESCRIPTION"; e.Request.Data.Properties.FileTypes.Add(".txt"); e.Request.Data.SetDataProvider(StandardDataFormats.StorageItems, OnDataReq); }
the OndataRequested method has to be completed very fast. so i think its timinig issue and maybe 8.1 bit faster dan 8.0?Microsoft Certified Solutions Developer - Windows Store Apps Using C#
- Marked as answer by Jamles HezModerator Friday, January 10, 2014 8:29 AM
Friday, January 3, 2014 2:58 PM
All replies
-
DataRequestDeferral deferral = e.Request.GetDeferral(); var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Utils\shareappemail.html"); var file2 = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("SCLogFile.txt"); var stream = await file.OpenReadAsync(); var rdr = new StreamReader(stream.AsStream()); var contents = await rdr.ReadToEndAsync(); DataPackage requestData = e.Request.Data; requestData.Properties.Title = "Bla"; requestData.Properties.Description = "BlablaBla"; // The description is optional. //requestData.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(contents)); List<IStorageItem> imageItems = new List<IStorageItem>(); imageItems.Add(file2); requestData.SetStorageItems(imageItems); deferral.Complete();
But if i comment out this line the file is being attached! WHY? HOW? But i need HTML content + attached file:requestData.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(contents));
Foma
Friday, January 3, 2014 9:02 AM -
maybe the tavlets are slower? try to use a dataprovider instead of setting the data directly.
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
- Edited by Dave SmitsMVP Friday, January 3, 2014 12:18 PM
Friday, January 3, 2014 12:18 PM -
It sounds interesting and could be something if not this thing:
Lets say i don't create HTML content:
DataRequestDeferral deferral = e.Request.GetDeferral(); var file2 = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("SCLogFile.txt"); DataPackage requestData = e.Request.Data; requestData.Properties.Title = "Bla"; requestData.Properties.Description = "BlablaBla"; // The description is optional.
requestData.SetText("SAMPLE TEXT"); List<IStorageItem> imageItems = new List<IStorageItem>(); imageItems.Add(file2); requestData.SetStorageItems(imageItems); deferral.Complete();
In this code sample message Title, description adn text aren't displayed. But attachments are added - it can't be a timing issue because title setting is being processed firstly, so if attachemnt is added than Title should be also displayed. But on PC all is fine
Foma
Friday, January 3, 2014 12:36 PM -
Dave,
Also, i have tried you'r solution, but ran into a problem that something isn't working:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e) { e.Request.Data.Properties.Title = "THIS IS A TITLE"; e.Request.Data.Properties.Description = "THIS IS A DESCRIPTION"; e.Request.Data.SetDataProvider(StandardDataFormats.StorageItems, OnDataReq); } internal async void OnDataReq(DataProviderRequest request) { var defferal = request.GetDeferral(); var file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("SCLogFile.txt"); request.SetData(RandomAccessStreamReference.CreateFromFile(file)); defferal.Complete(); }
And app says: There was a problem with data App1
Foma
Friday, January 3, 2014 12:58 PM -
Ok, now i have updated one of my tablets to Windows 8.1 and my first code works.
Does this mean that it was Windows 8.0 issue? And what should i say to customers? Sorry but Windows 8 is a bit buggy, and all of your Enterprice devices are useless - buy new ones???!!!
What the HELL?!
Foma
Friday, January 3, 2014 1:21 PM -
you need to set the filetypes property when using a dataprovider.
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e) { e.Request.Data.Properties.Title = "THIS IS A TITLE"; e.Request.Data.Properties.Description = "THIS IS A DESCRIPTION"; e.Request.Data.Properties.FileTypes.Add(".txt"); e.Request.Data.SetDataProvider(StandardDataFormats.StorageItems, OnDataReq); }
the OndataRequested method has to be completed very fast. so i think its timinig issue and maybe 8.1 bit faster dan 8.0?Microsoft Certified Solutions Developer - Windows Store Apps Using C#
- Marked as answer by Jamles HezModerator Friday, January 10, 2014 8:29 AM
Friday, January 3, 2014 2:58 PM -
you need to set the filetypes property when using a dataprovider.
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e) { e.Request.Data.Properties.Title = "THIS IS A TITLE"; e.Request.Data.Properties.Description = "THIS IS A DESCRIPTION"; e.Request.Data.Properties.FileTypes.Add(".txt"); e.Request.Data.SetDataProvider(StandardDataFormats.StorageItems, OnDataReq); }
the OndataRequested method has to be completed very fast. so i think its timinig issue and maybe 8.1 bit faster dan 8.0?
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Foma
Friday, January 3, 2014 6:10 PM -
Sorry, but that wasn't an answer! Dave addition helped me to get code working, but still, i have to choose between filled content or attached file! How my code, Dave's code works on Windows 8.1 but Windows 8 not
Foma
- Edited by Cheese1991 Friday, January 10, 2014 7:32 PM
Friday, January 10, 2014 7:32 PM