【问题标题】:Xamarin FilePicker blocks UserDialogXamarin FilePicker 阻止 UserDialog
【发布时间】:2020-06-27 16:02:24
【问题描述】:

我正在使用方法

srcPath = await CrossFilePicker.Current.PickFile();

来自 Xamarin.Plugin.FilePicker 包。这工作正常,我可以在我的设备上选择一个文件。之后我想通过

给用户一个反馈
 await UserDialogs.Instance.AlertAsync(message);

但是,在 Android Samsung SM-T805 上,对话消息被阻止。

在我看来,FilePicker 没有完全关闭。当达到 PickFile() 方法时,会出现两个窗口:一个名为 Android 的深色窗口,在确认对外部存储的访问之后,是实际的文件选择器。一旦我选择了一个文件,文件选择器就会消失,我的进一步代码就会被执行。但是背景层(深色,标题为 Android)直到我离开 Xamarin.Forms.Command 方法才消失,我将其链接到触发文件选取方法的按钮。

我的代码(大致):

[...]
using Xamarin.Forms;
using Plugin.FilePicker;
using Acr.UserDialogs;

namespace SomeNameSpace
{
    public class SomeViewModel
    {
        [...]
        public Command ImportCommand => new Command(() => ChooseFile());

        private async void ChooseFile()
        {
            string srcPath = await CrossFilePicker.Current.PickFile();
            await UserDialogs.Instance.AlertAsync("Help Me Please.");

            // Further Code
            [...]
        }
    }
}

有什么想法吗?提前致谢!

【问题讨论】:

  • 也可能是 UserDialogs 插件的问题,您可以将其替换为 Xamarin pop up with await DisplayAlert ("Alert", "You have been alerted", "OK");先排除这个可能的原因。
  • @NicoleLu,好主意,但行为保持不变。由于这个进一步的测试,我注意到我所在的当前线程卡在 await DisplayAlert/UserDialogs 方法中,因为我无法在警报/对话框上单击“确定”。对我来说,文件选择器似乎仍然是个问题。

标签: xamarin xamarin.forms xamarin.android filepicker


【解决方案1】:

Nicole Lu 给出了使用 DisplayAlert 而不是 UserDialogs 的提示。仅仅改变这种方法是不够的。但 DisplayAlert 可让您决定发送警报的页面。因此,诀窍是在使用 FilePicker 之前先保存当前页面,然后将警报发送到该页面。这是更新的代码:

[...]
using Xamarin.Forms;
using Plugin.FilePicker;
using Acr.UserDialogs;

namespace SomeNameSpace
{
    public class SomeViewModel
    {
        [...]
        public Command ImportCommand => new Command(() => ChooseFile());

        private async void ChooseFile()
        {
            Page page = App.Current.MainPage;
            string srcPath = await CrossFilePicker.Current.PickFile();
            await page.DisplayAlert("Help", "Please help me.", "OK");

            // Further Code
            [...]
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多