小编an2*_*n22的帖子

需要等待异步操作完成才能继续方法

在下面的方法中,我只希望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)

.net c# task async-await maui

1
推荐指数
1
解决办法
199
查看次数

标签 统计

.net ×1

async-await ×1

c# ×1

maui ×1

task ×1