2

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!

hjonas
  • 141
  • 10
  • It can also be problem with the UserDialogs plugin, you can replace it with Xamarin pop up with await DisplayAlert ("Alert", "You have been alerted", "OK"); to rule out this possible cause first. – Lia Mar 17 '20 at 09:24
  • @NicoleLu, good idea but the behavior remains the same. Due to this further test I've noticed that the current thread I am in gets stuck in the await DisplayAlert/UserDialogs method as I cannot click OK on the alert/dialog. Still seems like an issue with the filepicker to me. – hjonas Mar 17 '20 at 11:25

1 Answers1

1

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
            [...]
        }
    }
}
hjonas
  • 141
  • 10