Media.Plugin与Rg.Plugin结合使用:iOS一次只能激活一个操作吗?

时间:2019-03-14 08:39:42

标签: c# multithreading xamarin.forms xamarin.ios async-await

我有一个用Rg.Plugin.Popup.Pages创建的弹出窗口:

public partial class DynamicPopup : PopupPage
    {
       public DynamicPopup(bool showCancellButton, params (string btnName, Action btnAction)[] btns)
    {
        InitializeComponent();
        stkCancel.IsVisible = showCancellButton;

        foreach (var btn in btns)
        {
            var stack = new StackLayout
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Padding = new Thickness(0, 0, 0, 10)
            };

            stack.Children.Add(new CustomLabel
            {
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
                Text = btn.btnName,
                FontSize = 16,
                FontAttributes = FontAttributes.Bold
            });
            stack.GestureRecognizers.Add(new TapGestureRecognizer
            {
                Command = new Command(() =>
                {
                    btn.btnAction();
                    CloseAllPopup();
                })
            });

            stkParent.Children.Add(stack);             
            }
        }
    }
    }

简而言之,它接收(字符串,Action)元组的数组,并从元组创建具有相反的Text和Click事件的弹出按钮。 在另一页面中,我点击显示DynamicPopup的标签:

 lbImg.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(async() =>await AddPicture_Tapped()) });

然后我在AddPicture_Tapped中打开DynamicPopup:

 private async Task AddPicture_Tapped()
    {
        var popup = new DynamicPopup(true, ("Take Picture", Utility.TakePic), ("Choose From Gallery", Utility.ChoosePic));
        await Application.Current.MainPage.Navigation.PushPopupAsync(popup);
    }

Utility.TakePic:

public async static void TakePic()
        {
            try
            {
                var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
                var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

                if (cameraStatus != PermissionStatus.Granted || storageStatus != PermissionStatus.Granted)
                {
                    var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera, Permission.Storage });
                    cameraStatus = results[Permission.Camera];
                    storageStatus = results[Permission.Storage];
                }

                if (cameraStatus != PermissionStatus.Granted && storageStatus != PermissionStatus.Granted)
                {
                    await App.Current.MainPage.DisplayAlert("Permissions Denied", "Unable to take photo", "OK");
                    return;
                }

                //await CrossMedia.Current.Initialize();
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await App.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "OK");
                    return;
                }


                var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    Directory = "ExpertsParteners",
                    Name = "Item_Pic.jpg",
                    PhotoSize = PhotoSize.Small,
                    DefaultCamera = CameraDevice.Rear
                });

                if (file == null)
                {
                    return;
                }

                ImageData = new ImageData
                {
                    FilePath = GetFileName(file.Path),
                    FileFullPath = file.Path,
                    FileBase64 = Convert.ToBase64String(ReadToEnd(file.GetStream()))
                };

                file.Dispose();
            }
            catch (MediaPermissionException ex)
            {
                await App.Current.MainPage.DisplayAlert("Permission required", ex.Message, "OK");
            }
            catch (Exception ex)
            {
                await App.Current.MainPage.DisplayAlert("Error - Take", ex.Message, "OK");
                Debug.WriteLine(ex);
            }
        }

但是当我从弹出窗口中点击“拍照”时,相机第一次打开并自动关闭,而当我再次点击“拍照”时,会触发此异常:

  

System.InvalidOperationException:在以下位置只能激活一个操作   当时在Plugin.Media.MediaImplementation.GetMediaAsync   (UIKit.UIImagePickerControllerSourceType sourceType,System.String   mediaType,Plugin.Media.Abstractions.StoreCameraMediaOptions选项)   在<390f35b1db5e43b894d803204ceef8a9>:0中的[0x000d5]   Plugin.Media.MediaImplementation + d__22.MoveNext()   <390f35b1db5e43b894d803204ceef8a9>:0

中的[0x000fc]

该问题仅发生在iOS中,也可以在没有弹出窗口的情况下正常运行,例如从按钮的click事件执行Utility.TakePic。

编辑

我创建了一个新的带有静态控件的弹出窗口,并将两个方法TakePicChoosePic移到了它,现在它可以正常工作了,但是我仍然想使用动态弹出窗口并保留这些方法在Utility类中!

0 个答案:

没有答案