在Xamarin UWP应用程序中从“远程网络共享"打开文件

编程入门 行业动态 更新时间:2024-10-20 09:22:18
本文介绍了在Xamarin UWP应用程序中从“远程网络共享"打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以通过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="schemas.microsoft/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应用程序中从“远程网络共享"打开文件

本文发布于:2023-11-11 07:32:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577773.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   文件   网络   Xamarin   UWP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!