在下面的方法中,我只希望result在为结果赋值后对变量进行空检查。我认为现在发生的情况是,有时空检查是在FilePicker.PickAsync()完成之前发生的,因此结果尚未分配值。相反,我只希望在用户选择文件后进行空检查。
private async void PickFileButton_Clicked(object sender, EventArgs e)
{
try
{
Console.WriteLine("checkpoint1");
FileResult result = await FilePicker.PickAsync(new PickOptions
{
FileTypes = new FilePickerFileType(
new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { "public.item" } },
{ DevicePlatform.Android, new[] { "*/*" } },
{ DevicePlatform.WinUI, new[] { "*/*" } },
{ DevicePlatform.MacCatalyst, new[] { "public.item" } }
})
}); ;
Console.WriteLine("checkpoint2");
if (result.FileName != null)
{
filePath = result.FullPath;
FilePathLabel.Text = filePath;
Console.WriteLine(FilePathLabel.Text);
}
}
catch (Exception ex) …Run Code Online (Sandbox Code Playgroud)