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://docs.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".
Figured it out, following is the solution to my problem.
Added global variable in MainPage.xaml.cs:
In constructor of MainPage, added this
Up to this point, my code has not change but next comes call to ShowShareUI and that call was originally called from a background thread but dispatched to UI thread using Dispatcher (see original post). I still dont know why doing it as I explained originally was not working but after changing code like below, It is working now. So, initiate sharing on button click on the UI thread:
This event handler is triggered by the above call to ShowShareUI():
This has resolved my issue
In UWP, you could not get files from path directly.
So, your code would be changed like the following:
Please change your code and try to run your app again to see if you still face this issue.