在Xamarin UWP应用程序中从“远程网络共享"打开文件 [英] Open a file from Remote Network Share in Xamarin UWP application
问题描述
是否可以通过Xamarin UWP Application中的远程网络共享"打开文件.?
Is there any way to open a file from Remote Network Share in Xamarin UWP Application. ?
我们尝试使用Xamarin File Picker,但是它包含用户选择文件的方式.
We tried with Xamarin File Picker, but it includes user to select the file.
private void OpenFile()
{
FileData fileData = await CrossFilePicker.Current.PickFile();
string fileName = fileData.FileName;
string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
}
我们希望如果用户单击路径,那么文件将以读取模式显示.
We want that If the user clicks on the Path then the file will display in a Read Mode.
推荐答案
是否可以通过Xamarin UWP Application中的远程网络共享"打开文件.?
Is there any way to open a file from Remote Network Share in Xamarin UWP Application. ?
UWP提供了 broadFileSystemAccess
功能可以使用 Windows.Storage命名空间
中的API访问更大的文件.您需要在访问之前添加受限制的 broadFileSystemAccess
功能.
UWP has provided broadFileSystemAccess
capability to access broader file with APIs in the Windows.Storage namespace
. You need add the restricted broadFileSystemAccess
capability before access.
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
如果要在 .NET Standard
中获取文件,则需要创建 DependencyService
.
If you want to get the file in the .NET Standard
, you need create a DependencyService
.
在 .NET Standard
中创建文件访问界面.
Create file access interface in your .NET Standard
.
IFileAccess
public interface IFileAccess
{
Task<FileData> GetFileStreamFormPath(string Path);
}
public class FileData
{
public byte[] DataArray { get; set; }
public string FileName { get; set; }
public string FilePath { get; set; }
}
在本机UWP项目中实施 IFileAccess
接口.
Implement IFileAccess
interface in native UWP project.
FileAccessImplementation
[assembly: Xamarin.Forms.Dependency(typeof(FileAccessImplementation))]
namespace App6.UWP
{
public class FileAccessImplementation : IFileAccess
{
public async Task<FileData> GetFileStreamFormPath(string Path)
{
var file = await StorageFile.GetFileFromPathAsync(Path);
byte[] fileBytes = null;
if (file == null) return null;
using (var stream = await file.OpenReadAsync())
{
fileBytes = new byte[stream.Size];
using (var reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}
var FileData = new FileData()
{
FileName = file.Name,
FilePath = file.Path,
DataArray = fileBytes
};
return FileData;
}
}
}
用法
var file = DependencyService.Get<IFileAccess>().GetFileStreamFormPath(@"\\remote\folder\setup.exe");
这篇关于在Xamarin UWP应用程序中从“远程网络共享"打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!