我正在开发一个应用程序UWP其应能共享其文件。我跟着the documentation from Microsoft和解决方案工作得很好。
下面是我的实现:
public void ShareLocalFile(LocalFileToShare file)
{
DataTransferManager.GetForCurrentView().DataRequested += async (sender, args) =>
{
var deferral = args.Request.GetDeferral();
try
{
var storageFile = await StorageFile.GetFileFromPathAsync(file.FilePath).AsTask();
args.Request.Data.SetStorageItems(new[] { storageFile });
}
finally
{
deferral.Complete();
}
};
DataTransferManager.ShowShareUI();
}
然而,应用程序存储与不是人类可读的名字,这使得用户可以与不安共享的所有文件。所以,我想分享一个替代名称的文件实际上不重命名文件系统,因为文件被第三方阅读器打开了。此外,该文件是相当大的,并使用新名称复制他们是不是一个好的选择。
首先,我认为我可以做一个符号链接,但it's only possible with Administrator rights
然后我看着the signature of "void DataPackage.SetStorageItems(IEnumerable value)" method和猜测,这可能是可能通过有我自己的实现IStorageItem我就是这样做的:
public class StorageItemWithAlternativeName : IStorageItem
{
private readonly IStorageItem storageItem;
public StorageItemWithAlternativeName(IStorageItem storageItem, string alternativeItemName)
{
this.storageItem = storageItem;
Name = alternativeItemName;
}
public string Name { get; }
// the interface implementation omitted for briefness but it simply delegates all actions to the decorated storageItem
}
public static class LocalFileToShareExtensions
{
public static async Task<IStorageItem> GetStorageItem(this LocalFileToShare file)
{
var storageFile = await StorageFile.GetFileFromPathAsync(file.FilePath).AsTask();
if (!string.IsNullOrWhiteSpace(file.AlternativeFileName))
{
storageFile = new StorageItemWithAlternativeName(storageFile, file.AlternativeFileName);
}
return storageFile;
}
}
在这里,我失败了。该错误是相当愚蠢的 - SetStorageItems方法抛出一个InvalidCastException:“没有这样的接口支持的集合包含不能转换为只读形式的项目(S)。”
我调查了Windows事件日志,发现如下条目:
Faulting application name: [MyApp].Windows.exe, version: 7.0.0.0, time stamp: 0x5bb69bfe
Faulting module name: combase.dll, version: 10.0.17763.253, time stamp: 0xa3f81b2d
Exception code: 0xc000027b
Fault offset: 0x00209931
Faulting process id: 0x4ee4
Faulting application start time: 0x01d4be3ccca1f00f
Faulting application path: [PathToMyApp].Windows.exe
Faulting module path: C:\WINDOWS\System32\combase.dll
Report Id: 35999df1-6b4f-4675-a821-a84e6ea0cfb4
Faulting package full name: [MyAppPackageName]
Faulting package-relative application ID: App
看来,DataPackage对象与COM通信,所以我也试过[assembly: [ComVisible(true)]
我集属性,但我没有成功。
问题是,我该怎么样的哑巴的系统,并用不同的名称共享文件?是否有可能通过自己的实现来UWP SDK的方法呢?因为现在它违反the Liskov substitution principle。
我会感谢任何帮助!
请尽量使用重载的方法SetStorageItems
与参数readOnly:false
,是这样的:
args.Request.Data.SetStorageItems(new[] { storageFile }, false);