I am trying to share a file from my UWP app sandbox running on Windows 10 desktop.
Following MS documentation on this page, the implementation seem to be fairly straight forward; however, I am having issues https://learn.microsoft.com/en-us/windows/uwp/app-to-app/share-data
I created DataTransferManager and attached DataRequested event in c-tor of my class as per explanation in the article:
DataTransferManager dataTransferManager;
public MainPage()
{
this.InitializeComponent();
...
dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
}
Next, in a method that is called from background thread, I call ShowShareUI making sure it executes on the main thread
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
DataTransferManager.ShowShareUI(); //THIS CALL SHOWS A POPUP AND IMMEDIATELLY CLOSES IT
}).AsTask().Wait();
Then in my OnDataRequested event, I add the file I want to share:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
// get the file from application sandbox
StorageFile attachmentFile = await StorageFile.GetFileFromPathAsync(@"C:\Users\ME\AppData\Local\Packages\f040f23f-....-84\LocalState\logs\MYLOG.log");
DataRequest request = args.Request;
request.Data.Properties.Title = "My Log File";
request.Data.Properties.Description = "Sharing MYLOG file.";
List<IStorageItem> storage = new List<IStorageItem>()
{
attachmentFile
};
request.Data.SetStorageItems(storage);
}
but nothing happens. I had no chance to select anything in the dialog that opened for a 1/2 second and closed. Here is how the dialog looks like, it opens and closes almost immediately after it opened, it just show "This app can't share right now".