UWP DataTransferManager ShowShareUI() 打开与 "This app can't share right now" 的共享对话框并在之后立即关闭它
UWP DataTransferManager ShowShareUI() Opens Sharing Dialog with "This app can't share right now" and Closes it Immediately After
我正在尝试在 Windows 10 桌面上共享我的 UWP 应用程序沙箱 运行 中的文件。
遵循此页面上的 MS 文档,实施似乎相当简单;但是,我遇到了问题 https://docs.microsoft.com/en-us/windows/uwp/app-to-app/share-data
我根据文章中的解释在 class 的 c-tor 中创建了 DataTransferManager 并附加了 DataRequested 事件:
DataTransferManager dataTransferManager;
public MainPage()
{
this.InitializeComponent();
...
dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
}
接下来,在从后台线程调用的方法中,我调用 ShowShareUI 确保它在主线程上执行
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
DataTransferManager.ShowShareUI(); //THIS CALL SHOWS A POPUP AND IMMEDIATELLY CLOSES IT
}).AsTask().Wait();
然后在我的 OnDataRequested 事件中,我添加了我要共享的文件:
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);
}
但是没有任何反应。我没有机会在打开 1/2 秒并关闭的对话框中 select 任何内容。这是对话框的样子,它打开后几乎立即打开和关闭,它只显示 "This app can't share right now".
StorageFile attachmentFile = await StorageFile.GetFileFromPathAsync(@"C:\Users\ME\AppData\Local\Packages\f040f23f-....-84\LocalState\logs\MYLOG.log");
在 UWP 中,您无法直接从路径获取文件。
UWP apps run sandboxed and have very limited access to the file system. For the most part, they can directly access only their install folder and their application data folder. They do not have permission to access the file system elsewhere. Rob has explained this point in his blog Skip the path: stick to the StorageFile
因此,您的代码将更改如下:
StorageFolder logsFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("logs");
StorageFile file = await logsFolder.GetFileAsync("MYLOG.log");
请更改您的代码并再次尝试 运行 您的应用,看看您是否仍然遇到此问题。
想通了,下面是我的问题的解决方案。
在MainPage.xaml.cs中添加了全局变量:
private DataTransferManager dataTransferManager;
在 MainPage 的构造函数中,添加了这个
dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
到目前为止,我的代码没有改变,但接下来是对 ShowShareUI 的调用,该调用最初是从后台线程调用的,但使用 Dispatcher 分派到 UI 线程(请参阅原始post)。我仍然不知道为什么按照我最初解释的那样做是行不通的,但是在像下面这样更改代码之后,它现在可以工作了。因此,在 UI 线程上单击按钮启动共享:
private void Button_Click()
{
DataTransferManager.ShowShareUI();
}
此事件处理程序由上述对 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();
});
});
}
这已经解决了我的问题
我正在尝试在 Windows 10 桌面上共享我的 UWP 应用程序沙箱 运行 中的文件。
遵循此页面上的 MS 文档,实施似乎相当简单;但是,我遇到了问题 https://docs.microsoft.com/en-us/windows/uwp/app-to-app/share-data
我根据文章中的解释在 class 的 c-tor 中创建了 DataTransferManager 并附加了 DataRequested 事件:
DataTransferManager dataTransferManager;
public MainPage()
{
this.InitializeComponent();
...
dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
}
接下来,在从后台线程调用的方法中,我调用 ShowShareUI 确保它在主线程上执行
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
DataTransferManager.ShowShareUI(); //THIS CALL SHOWS A POPUP AND IMMEDIATELLY CLOSES IT
}).AsTask().Wait();
然后在我的 OnDataRequested 事件中,我添加了我要共享的文件:
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);
}
但是没有任何反应。我没有机会在打开 1/2 秒并关闭的对话框中 select 任何内容。这是对话框的样子,它打开后几乎立即打开和关闭,它只显示 "This app can't share right now".
StorageFile attachmentFile = await StorageFile.GetFileFromPathAsync(@"C:\Users\ME\AppData\Local\Packages\f040f23f-....-84\LocalState\logs\MYLOG.log");
在 UWP 中,您无法直接从路径获取文件。
UWP apps run sandboxed and have very limited access to the file system. For the most part, they can directly access only their install folder and their application data folder. They do not have permission to access the file system elsewhere. Rob has explained this point in his blog Skip the path: stick to the StorageFile
因此,您的代码将更改如下:
StorageFolder logsFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("logs");
StorageFile file = await logsFolder.GetFileAsync("MYLOG.log");
请更改您的代码并再次尝试 运行 您的应用,看看您是否仍然遇到此问题。
想通了,下面是我的问题的解决方案。
在MainPage.xaml.cs中添加了全局变量:
private DataTransferManager dataTransferManager;
在 MainPage 的构造函数中,添加了这个
dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
到目前为止,我的代码没有改变,但接下来是对 ShowShareUI 的调用,该调用最初是从后台线程调用的,但使用 Dispatcher 分派到 UI 线程(请参阅原始post)。我仍然不知道为什么按照我最初解释的那样做是行不通的,但是在像下面这样更改代码之后,它现在可以工作了。因此,在 UI 线程上单击按钮启动共享:
private void Button_Click()
{
DataTransferManager.ShowShareUI();
}
此事件处理程序由上述对 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();
});
});
}
这已经解决了我的问题