Forum Xamarin Xamarin.Forms

Xamarin.Plugin.FilePicker

SwanneSwanne Member ✭✭

Hi all,

I am using the plugin mentioned in the title and getting quite frustrated with the following behavior.

I have a number of buttons the home screen which each have 2 functions. Quick press = play the video, press and hold = use file picker to choose which video file is associated to the button.

The problem I am having is that whichever button I long press and select video file for becomes the only updatable file path. For example, long press Button_1 and use the file picker to select Video_1. Short press on Button_1 plays the correctly chosen video file. Then when I long press on Button_2 and select a new video file, it updates Button_1, not 2. So frustrating.

To enable the short vs long press functionality, I used the Stoppable Timer solution from here: https://forums.xamarin.com/discussion/comment/149877/#Comment_149877

Code snippet is here:
[code]

region Button 1 Handler

    public void OnButtonReleased_1()
    {
        if (stoppableTimer != null)
        {
            stoppableTimer.Stop();
        }

        string[] videoList = _videoFileManager.GetVideoPaths();
        var page = new VideoPage(videoList[0]);
        Navigation.PushAsync(page);
        NavigationPage.SetHasNavigationBar(page, false);
    }

    public void OnButtonPressed_1()
    {
        if (stoppableTimer == null)
        {
            stoppableTimer = new StoppableTimer(TimeSpan.FromSeconds(2), VideoAdmin_1);
            stoppableTimer.Start();
        }
        else
        {
            stoppableTimer.Start();
        }
    }

    public async void VideoAdmin_1()
    {

        string[] options = new string[] { "Change Button Name", "Select a new Video" };
        var selectedOption = await DisplayActionSheet("What would you like to do?", "Cancel", "", options);
        if (selectedOption == options[0])
        {

        }
        else if (selectedOption == options[1])
        {
            try
            {
                var file1 = await CrossFilePicker.Current.PickFile();
                if (file1 == null)
                    return;
                _videoFileManager.WriteVideoFiles(0, file1.FilePath);
                stoppableTimer.Stop();

            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Exception choosing file: " + ex.ToString());
            }


        }




    }
    #endregion

    #region Button 2 Handler
    public void OnButtonReleased_2()
    {
        if (stoppableTimer != null)
        {
            stoppableTimer.Stop();
        }

        string[] videoList = _videoFileManager.GetVideoPaths();
        var page = new VideoPage(videoList[1]);
        Navigation.PushAsync(page);
        NavigationPage.SetHasNavigationBar(page, false);
    }

    public void OnButtonPressed_2()
    {
        if (stoppableTimer == null)
        {
            stoppableTimer = new StoppableTimer(TimeSpan.FromSeconds(2), VideoAdmin_2);
            stoppableTimer.Start();
        }
        else
        {
            stoppableTimer.Start();
        }
    }

    public async void VideoAdmin_2()
    {
        try
        {
            var file1 = await CrossFilePicker.Current.PickFile();
            if (file1 == null)
                return;
            _videoFileManager.WriteVideoFiles(0, file1.FilePath);
            stoppableTimer.Stop();

        }
        catch (Exception ex)
        {
            System.Console.WriteLine("Exception choosing file: " + ex.ToString());
        }                                                               

    }

    #endregion

[/code]

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo so that we can test with it ?

Sign In or Register to comment.