Search code examples
xamarinxamarin.formsxamarin.androidfilepicker

Xamarin FilePicker blocks UserDialog


I am using the method

srcPath = await CrossFilePicker.Current.PickFile();

from the package Xamarin.Plugin.FilePicker. This works fine and I can select a file on my device. Afterwards I want to give the user a feedback via

 await UserDialogs.Instance.AlertAsync(message);

However, on Android Samsung SM-T805, the dialog message is blocked.

It seems to me that the FilePicker is not fully closed. When the PickFile() method is reached two windows appear: A dark one titled Android and, after confirming the access to external storage, the actual file picker. Once I've chosen a file the file picker disappears and my further code is executed. But the background layer (dark, titled Android) does not disappear until I leave the Xamarin.Forms.Command method, which I linked to a button triggering the file picking method.

My code (roughly):

[...]
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
            [...]
        }
    }
}

Any ideas? Thanks in advance!


Solution

  • Nicole Lu gave the hint of using DisplayAlert instead of UserDialogs. Simply changing this method was not enough. But DisplayAlert lets you decide on which Page to send the alert. Thus the trick was to first save the current page before using the FilePicker and later sending the alert to that page. Here is the updated code:

    [...]
    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
                [...]
            }
        }
    }