示例#1
0
        public PicSelectorViewModel(Page page, PhotoSelectorOptions options)
        {
            this.options  = options;
            CancelCommand = new Command(async() =>
            {
                CropViewModel.PicSelectionResult.UserCancelled = true;
                await Application.Current.MainPage.Navigation.PopModalAsync();
            });
            DoneCommand = new Command(async() =>
            {
                CropViewModel.PicSelectionResult.UserCancelled = false;
                await Application.Current.MainPage.Navigation.PopModalAsync();
            });
            ChangePhotoCommand = new Command(async fake =>
            {
                await CrossMedia.Current.Initialize();

                var canTakePhoto      = CrossMedia.Current.IsTakePhotoSupported;
                var canSelectPhoto    = CrossMedia.Current.IsPickPhotoSupported;
                string selectedOption = null;
                if (canTakePhoto && canSelectPhoto)
                {
                    var buttonText = new[] { options.TakePhotoText, options.SelectPhotoText };
                    selectedOption = await page.DisplayActionSheet(null, CancelText, null, buttonText);
                }
                else if (canTakePhoto)
                {
                    selectedOption = options.TakePhotoText;
                }
                else if (canSelectPhoto)
                {
                    selectedOption = options.SelectPhotoText;
                }
                else
                {
                    //TODO: How to internationalize these strings
                    await page.DisplayAlert("No Photo Support", "This device does not support taking or selecting photos", "Ok");
                    selectedOption = null;
                }

                MediaFile selected = null;
                if (selectedOption == options.TakePhotoText)
                {
                    selected = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions());
                }
                else if (selectedOption == options.SelectPhotoText)
                {
                    selected = await CrossMedia.Current.PickPhotoAsync();
                }
                if (selected != null)
                {
                    var pic = await PicManager.LoadAsync(new Uri(selected.Path));
                    CropViewModel.LoadImage(pic);
                    BugHound.Info($"Setting selected photo to: {selected.Path}");
                }
            });
        }
示例#2
0
        public static async Task ShowAsync(PhotoSelectorOptions options, Action <PicSelectionResult> onCompletionHandler)
        {
            var page = new ContentPage();
            var vm   = new PicSelectorViewModel(page, options);
            await vm.InitializeAsync();

            page.Content = new PicSelectorView(vm);
            await Application.Current.MainPage.Navigation.PushModalAsync(page);

            // after the page is done, go ahead and run the 'onCompleteHandler' to store the user-selected image
            page.Disappearing += (sender, args) => onCompletionHandler(vm.CropViewModel.PicSelectionResult);
        }