【问题标题】:Example code for Camera and Gallery access using Acr.XamForms Nuget in Xamarin.Forms [closed]在 Xamarin.Forms 中使用 Acr.XamForms Nuget 访问相机和图库的示例代码 [关闭]
【发布时间】:2014-09-22 12:33:22
【问题描述】:

谁能提供在 Xamarin.Forms 中使用 Acr.XamForms Nuget 访问相机和图库的链接/示例代码?

我有来自 here 的代码,但我想在我的 PCL 中执行功能使用 Acr.XamForms nuget 包 [而不是将其包含在设备特定项目中]

提前致谢!

【问题讨论】:

    标签: android ios xamarin xamarin.forms


    【解决方案1】:

    【讨论】:

    • 我确实有这个项目,但是它在一个项目中包含了很多东西,并且包含了很多类,因此正在寻找相同的代码的简单解决方案。
    【解决方案2】:

    我花了一些时间研究 ACR 示例,但仍然没有完全弄清楚它们是如何组合在一起的。所以我最终从头开始,对 App.cs 文件中的所有内容进行编码,并通过反复试验想出了这个适用于 iOS 和 Android 的非常简单的解决方案。它允许您从图库中获取图像或拍照,完成后会显示照片。希望这可以帮助某人节省几个小时。

    我还想向所有努力为社区创建优秀库的人发出请求:请提供超级简单的使用示例。不要创建一个详尽的示例来演示您的库的所有内容...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using Xamarin.Forms;
    using Acr.XamForms.Mobile.Media;
    
    namespace photoSimpleTest
    {
        public class App : Application
        {
            MediaPicker mPick = new MediaPicker();
    
            Image ShowPic;
    
            public App ()
            {
                var showGalleryButton = new Button {
                    Text="Show Gallery"
                };
                showGalleryButton.Clicked += ShowGallery;
    
                var takePictureButton = new Button
                {
                    Text = "Take Picture"
                };
                takePictureButton.Clicked += TakePicture;
    
                ShowPic = new Image();
    
                MainPage = new ContentPage
                {
                    Content = new StackLayout
                    {
                        VerticalOptions = LayoutOptions.Center,
                        Children = {
                            showGalleryButton,
                            takePictureButton,
                            ShowPic
                        }
                    }
                };
            }
    
            public async void ShowGallery(object s, EventArgs a) 
            {
                if (!mPick.IsPhotoGalleryAvailable)
                    Debug.WriteLine("Photo Gallery is unavailable");
                else
                {
                    var result = await mPick.PickPhoto();
                    PhotoReceived(result);
                }
            }
    
            public async void TakePicture(object s, EventArgs a)
            {
                if (!mPick.IsCameraAvailable)
                    Debug.WriteLine("Camera is not available");
                else
                {
                    var opt = new CameraOptions { };
                    var result = await mPick.TakePhoto(opt);
                    PhotoReceived(result);
                }
            }
    
            private void PhotoReceived(IMediaFile file)
            {
                if (file == null)
                    Debug.WriteLine("Photo Cancelled");
                else
                    ShowPic.Source = ImageSource.FromFile(file.Path);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2018-11-12
      • 2010-09-08
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      相关资源
      最近更新 更多