C# Xamarin,Plugin.FilePicker->;例外情况:“…”;。。。视图=DecorView
在我的Xamarin项目中,我使用Plugin.FilePicker,除了一个例外,其他一切都正常 如果执行从2到6次选择文件的操作,并且单击后退按钮的次数相同,则会出现异常:C# Xamarin,Plugin.FilePicker->;例外情况:“…”;。。。视图=DecorView,c#,xamarin,xamarin.android,C#,Xamarin,Xamarin.android,在我的Xamarin项目中,我使用Plugin.FilePicker,除了一个例外,其他一切都正常 如果执行从2到6次选择文件的操作,并且单击后退按钮的次数相同,则会出现异常: Unhandled Exception: Java.Lang.IllegalArgumentException: View=DecorView@b10016d[] not attached to window manager 使用插件的类完全由try、catch块组成。在另一个类中,我使用了前一个类的方法,我还配备了
Unhandled Exception:
Java.Lang.IllegalArgumentException: View=DecorView@b10016d[] not attached to window manager
使用插件的类完全由try、catch块组成。在另一个类中,我使用了前一个类的方法,我还配备了try,catch块-仅用于检查。它不会捕获任何地方的异常
该项目是在Android和UWP上编写的。它发生在android上,但在UWP上没有
有什么想法吗
编辑
谢谢回答:)这是我代码的一部分:
public class FilePickerService
{
private FileData fileData;
public FilePickerService()
{
fileData = new FileData();
}
public async Task<bool> pickFile()
{
try
{
fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return false; // user canceled file picking
else
{
string fileName = fileData.FileName;
string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
System.Console.WriteLine("File name chosen: " + fileName);
System.Console.WriteLine("File data: " + contents);
return true;
}
}
catch (Exception ex)
{
System.Console.WriteLine("Exception choosing file: " + ex.ToString());
return false;
}
}
public FileData getFileData()
{
try
{
if (fileData != null)
{
return fileData;
}
else
{
return null;
}
}
catch(Exception ex)
{
var error = ex.Message;
return null;
}
}
public string getFileName()
{
try
{
string fileName = fileData.FileName;
return fileName;
}
catch(Exception ex)
{
var error = ex.Message;
return "";
}
}
public string getFileContents()
{
try
{
string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
return contents;
}
catch (Exception ex)
{
var error = ex.Message;
return "";
}
}
public bool fileIsPicture()
{
try
{
string fileName = fileData.FileName;
bool isJPG = false;
bool isPNG = false;
isJPG = Path.GetExtension(fileName).Equals(".jpg", StringComparison.InvariantCultureIgnoreCase) || Path.GetExtension(fileName).Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase);
isPNG = Path.GetExtension(fileName).Equals(".png", StringComparison.InvariantCultureIgnoreCase);
if (isJPG == true || isPNG == true)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
var error = ex.Message;
return false;
}
}
public bool fileIsExist()
{
try
{
if (fileData != null)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
var error = ex.Message;
return false;
}
}
}
类似的代码可用于选择3个文件。下面我给出了三分之一的支持
public Image imagePhotoOne;
public Image imageTakePhotoOne;
private Service.FilePickerService serviceImagePickerOne;
//Another code...
imagePhotoOne = new Image();
imageTakePhotoOne = new Image();
serviceImagePickerOne = new Service.FilePickerService();
//Another code...
TapGestureRecognizer tapGestureImageTakePhotoOne = new TapGestureRecognizer();
tapGestureImageTakePhotoOne.Tapped += async (sender, e) =>
{
try
{
var status = await serviceImagePickerOne.pickFile();
if (status == true)
{
if (serviceImagePickerOne.fileIsPicture() == true)
{
imagePhotoOne.IsVisible = true;
imageTakePhotoOne.IsVisible = false;
imagePhotoOne.Source = ImageSource.FromStream(() =>
{
return new MemoryStream(serviceImagePickerOne.getFileData().DataArray);
});
}
else
{
await Task.Delay(100);
UserDialogs.Instance.Toast("Zdjecie nie jest w formacje .jpg/.jpeg/.png");
await Task.Delay(100);
imagePhotoOne.IsVisible = false;
imageTakePhotoOne.IsVisible = true;
imagePhotoOne.Source = null;
}
}
else
{
await Task.Delay(100);
UserDialogs.Instance.Toast("Nie wybrano pliku");
await Task.Delay(100);
}
}
catch(Exception ex)
{
var error = ex.Message;
}
};
imageTakePhotoOne.GestureRecognizers.Add(tapGestureImageTakePhotoOne);
public Label labelFileOne;
public Image imageTakeFileOne;
private Service.FilePickerService serviceFilePickerOne;
//Code create elements
TapGestureRecognizer tapGestureImageTakeFileOne = new TapGestureRecognizer();
tapGestureImageTakeFileOne.Tapped += async (sender, e) =>
{
try
{
var status = await serviceFilePickerOne.pickFile();
if (status == true)
{
if (serviceFilePickerOne.getFileData() != null)
{
labelFileOne.IsVisible = true;
labelFileOne.Text = serviceFilePickerOne.getFileName();
imageTakeFileOne.IsVisible = false;
}
else
{
await Task.Delay(100);
UserDialogs.Instance.Toast("Nie udało się wczytać pliku");
await Task.Delay(100);
labelFileOne.IsVisible = false;
labelFileOne.Text = "";
imageTakeFileOne.IsVisible = false;
}
}
else
{
await Task.Delay(100);
UserDialogs.Instance.Toast("Nie wybrano pliku");
await Task.Delay(100);
}
}
catch(Exception ex)
{
var error = ex.Message;
}
};
imageTakeFileOne.GestureRecognizers.Add(tapGestureImageTakeFileOne);
当我使用Plugin.FilePicker 3到6次时会发生错误,但我不会每次都选择任何文件。答案编辑 看起来您需要媒体选择器,因为您只选择png和jpeg文件。我复制了你正在使用的插件的问题。请改用xam.plugin.media,它会解决您的问题