UWP DataTransferManager ShowShareUI()使用“此应用无法立即共享”打开“共享”对话框。并在之后立即关闭 [英] UWP DataTransferManager ShowShareUI() Opens Sharing Dialog with "This app can't share right now" and Closes it Immediately After

查看:159
本文介绍了UWP DataTransferManager ShowShareUI()使用“此应用无法立即共享”打开“共享”对话框。并在之后立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Windows 10桌面上运行的UWP应用沙箱共享文件。



按照本页上的MS文档,实现似乎很简单向前;但是,我遇到问题

解决方案

弄清楚了,以下是我的问题的解决方案。



在MainPage.xaml.cs中添加了全局变量:

  private DataTransferManager dataTransferManager; 

在MainPage的构造函数中,添加了

  dataTransferManager = DataTransferManager.GetForCurrentView(); 
dataTransferManager.DataRequested + = new TypedEventHandler< DataTransferManager,DataRequestedEventArgs>(this.OnDataRequested);

到目前为止,我的代码尚未更改,但接下来调用ShowShareUI,该调用最初是从后台线程调用,但使用Dispatcher调度到UI线程(请参阅原始帖子)。我仍然不知道为什么这样做(如我最初解释的那样)不起作用,但是在更改了如下所示的代码之后,它现在正在起作用。因此,在UI线程上单击按钮开始共享:

  private void Button_Click()
{
DataTransferManager.ShowShareUI();
}

此事件处理程序由上述对ShowShareUI()的调用触发:

 私有异步void DataTransferManager_DataRequested(DataTransferManager sender,DataRequestedEventArgs args)
{
DataRequestDeferral deferral = args.Request.GetDeferral ();

// MyLogger具有SendLogs方法,但在上面的Button_Click中调用此方法将不起作用,并且共享
//对话框只会打开和关闭。将其移至该事件即可解决此问题。
MyLogger.SendLogs(异步日志路径=>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,async()=>
{
StorageFile myFile =等待StorageFile.GetFileFromPathAsync(logpath);

DataRequest请求= args.Request;
request.Data.Properties.Title =共享我的日志;
request.Data.Properties.Description = string.Format(共享日志文件{0}。,myFile.DisplayName);

List< IStorageItem> myStorageItems =新列表< IStorageItem>(){myFile };
request.Data.SetStorageItems(myStorageItems);
deferral.Complete();
});
});
}

这已解决了我的问题


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:

private DataTransferManager dataTransferManager;

In constructor of MainPage, added this

dataTransferManager = DataTransferManager.GetForCurrentView(); 
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested); 

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:

private void Button_Click()
{
  DataTransferManager.ShowShareUI();
}

This event handler is triggered by the above call to ShowShareUI():

private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    DataRequestDeferral deferral = args.Request.GetDeferral();

    // MyLogger has SendLogs method but calling this in Button_Click above will not work and the Share
    // dialog will just open and close.  Moving it down to this event solves the issue.
    MyLogger.SendLogs(async logpath =>
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
        {
            StorageFile myFile = await StorageFile.GetFileFromPathAsync(logpath);

            DataRequest request = args.Request; 
            request.Data.Properties.Title = "Share My Logs";
            request.Data.Properties.Description = string.Format("Share log file {0}.", myFile.DisplayName);

            List<IStorageItem> myStorageItems = new List<IStorageItem>() { myFile };
            request.Data.SetStorageItems(myStorageItems);
            deferral.Complete();
        });
    });
}

This has resolved my issue

这篇关于UWP DataTransferManager ShowShareUI()使用“此应用无法立即共享”打开“共享”对话框。并在之后立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆