嗨,我是Xamarin的新手,我希望你們能幫助我。由于Xamarin中沒有一個默認的檔案夾選取器,我想自己實作它。問題是,UWP和Android都向我拋出了這樣的例外:
System.Unautut.com
System.UnauthorizedAccessException HResult=0x80070005 訊息 = C:UsersimttAppDataLocalPackages3ef1aa30-7ffe-4ece-84c7-d2eaf1f8634b_wvdsmkc2tee92LocalStateTest199.jpg'路徑的訪問被拒絕。 查詢器 = System.IO.FileSystem 釘子戶。 bei System.IO.FileSystem.DeleteFile(String fullPath) bei System.IO.File.Delete(String path) bei MinimalReproducibleExample.ViewModel.DeleteFiles() in C:Usersimttsource eposMinimalReproducibleExampleMinimalReproducibleExampleViewModel.cs。Zeile107 bei Xamarin.Forms.Command.<>c__DisplayClass4_0.<.ctor>b__0(Object o) bei Xamarin.Forms.Command.Execute(Object parameter) bei Xamarin.Forms.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager) bei Xamarin.Forms.Button.SendClicked() bei Xamarin.Forms.Platform.UWP.ButtonRenderer.OnButtonClick(Object sender, RoutedEventArgs e)
下面是xaml:
<?xml version="1.0" encoding="utf-8" ? >
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"。
x:Class="MinimalReproducibleExample.MainPage">/span>
<StackLayout>/span>
< Button Text="Add Image" Command="{Binding AddImage}" />
< 按鈕 文本="洗掉圖片" 命令="{Binding DeleteImages}"/span>/>
<Image Source="{Binding CreatedImage}"/span>/>/span>
</StackLayout>/span>
下面是后臺代碼:
using Xamarin.Forms;
namespace MinimalReproducibleExample
{
public partial class MainPage : ContentPage >。
{
public MainPage()
{
BindingContext = new ViewModel();
InitializeComponent()。
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
使用System.Linq.Definition; 使用System.Linq.Definition
using System.Runtime.CompilerServices;
namespace MinimalReproducibleExample[/span
{
public class ViewModel : INotifyPropertyChanged {
{
private ImageSource image;
private string fileFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Test") 。
public ICommand AddImage { get; }
public ICommand DeleteImages { get; }
public event PropertyChangedEventHandler PropertyChanged。
protected void OnPropertyChanged([CallerMemberName] string name = ")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name))。
}
public ViewModel()?
{
AddImage = new Command(ShowFilePicker)。
DeleteImages = new Command(DeleteFiles);
}
public ImageSource CreatedImage
{
get => image;
set
{
image = value;
OnPropertyChanged();
}
}
public async void ShowFilePicker()
{
FilePickerFileType filePickerFileType = new FilePickerFileType(
new Dictionary<DevicePlatform, IEnumerable<string> > {
{ DevicePlatform. iOS, new [] { "jpeg"/span>, "png"/span>, "mp3"/span>, "mpeg4Movie", "text", "utf8PlainText", "html" }. },
{ DevicePlatform. Android, new [] { "image/jpeg", "image/png", "audio/mp3", "audio/mpeg"/span>, "video/mp4"/span>, "text/*"/span>, "text/html"/span> }. },
{ DevicePlatform.UWP, new []{ "*.jpg"/span>, "*.jpeg"/span>, "*. png"/span>, "*.mp3"/span>, "*.mp4"/span>, "*.txt"/span>, "*.html"/span> } }
});
PickOptions pickOptions = new PickOptions
{
PickerTitle = "W?hlen Sie eine oder mehrere Dateien aus"/span>,
FileTypes = filePickerFileType,
};
IEnumerable<FileResult> pickedFiles = await FilePicker.PickMultipleAsync(pickOptions)。
List<FileResult> results = pickedFiles.ToList()。
if (results != null && results.Count > 0)
{
foreach (FileResult fileResult in results)
{
using (Stream stream = await fileResult.OpenReadAsync() )
{
DirectoryInfo directoryInfo = Directory.CreateDirectory(fileFolder)。
string directoryPath = directoryInfo.FullName;
string filepath = Path.Combine(directoryPath, fileResult.FileName);
try
{
byte[] bArray = new byte[stream.Length] 。
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate)
{
stream.Read(bArray, 0, (int)stream.Length) 。
int length = bArray.Length;
fs.Write(bArray, 0, length);
}
CreatedImage = ImageSource.FromFile(filepath)。
}
catch (Exception exc)
{
}
}
}
}
}
public void DeleteFiles()
{
string[] filePaths = Directory.GetFiles(fileFolder)。
foreach(string filePath in filePaths)
{
檔案.洗掉(filePath)。
}
}
}
}
我已經通過windows設定給了我的應用程式對檔案系統的訪問權限,我也給了Android部分讀寫權限。我甚至給了UWP部分 "broadFileAccess",但即使如此也沒有成功。
這與另一個問題相交,UWP部分可以將檔案寫入 "Environment.SpecialFolder.LocalApplicationData "中的一個檔案夾,但它不允許洗掉該檔案夾中的檔案。
這是否與UWP和Android的沙盒有關?
uj5u.com熱心網友回復:
應用程式無法進一步處理Xamarin中的磁盤/檔案夾
我用你的代碼進行了測驗,問題是當你洗掉檔案時,該檔案被影像控制元件使用,如果我們禁用
CreatedImage = ImageSource.FromFile(filepath);這一行,它將如期作業。我需要那個影像控制元件來顯示影像
我們建議你用流來渲染影像,但不要直接從檔案中創建源。
例如
CreatedImage = ImageSource.FromStream(() => stream)。轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/317275.html
標籤:
下一篇:從VisualStudio(Xamarin)使用GooglePlayStore簽名時總是收到"無效客戶端"的錯誤。
