XamariniForms,拍照,发生了未处理的异常

时间:2017-05-23 19:54:55

标签: c# android visual-studio xamarin xamarin.forms

我想在Xamarin.Forms拍一张照片。但是当我构建它时,当我点击“拍照”按钮时,我得到了上述错误。 我在所有线路上都设置了一个断点,但我找不到我的错。

enter image description here

点击拍照enter image description here

ResimYukle.axml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using hackathon.CustomControls;
using hackathon.Views;
using Plugin.Media;
namespace hackathon.TabbedPages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ResimYukle : ContentPage
    {
        private Image img;

        public ResimYukle()
        {
            InitializeComponent();
            RelativeLayout layout = new RelativeLayout();
            CustomButton btnTakePhoto = new CustomButton
            {
                Text = "Take Photo"
            };
            btnTakePhoto.Clicked += BtnTakePhoto_Clicked;

            CustomButton btnPickPhoto = new CustomButton
            {
                Text = "Pick Photo"
            };
            btnPickPhoto.Clicked += BtnPickPhoto_Clicked;
            CustomButton btnTakeVideo = new CustomButton
            {
                Text = "Take Video"
            };
            btnTakeVideo.Clicked += BtnTakeVideo_Clicked;
            CustomButton btnPickVideo = new CustomButton
            {
                Text = "Pick Vİdeo"
            };
            btnPickVideo.Clicked += BtnPickVideo_Clicked;

            StackLayout stkImage = new StackLayout
            {
                BackgroundColor = Color.White
            };
            img = new Image
            {
                Source = "defaultimg.png"
            };
            stkImage.Children.Add(img);

            layout.Children.Add(stkImage, Constraint.Constant(0),
                Constraint.Constant(0), Constraint.RelativeToParent(
                    (parent) =>
                    {
                        return parent.Width;
                    }));

            StackLayout stkPictureButtons = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Padding = 20,
                Children =
                {
                    btnTakePhoto,
                    btnPickPhoto
                }
            };
            StackLayout stkVideoButtons = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Padding = 20,
                Children =
                {
                    btnTakeVideo,
                    btnPickVideo,
                }
            };

            layout.Children.Add(stkPictureButtons, Constraint.Constant(0),
                Constraint.Constant(0), Constraint.RelativeToParent((parent) =>
                {
                    return parent.Width;
                }));

            layout.Children.Add(stkVideoButtons, Constraint.Constant(0),
                Constraint.RelativeToView(stkPictureButtons,
                (parent, sibling) =>
                {
                    return sibling.Height + 10;
                }), Constraint.RelativeToParent((parent) =>
                {
                    return parent.Width;
                }));

            Content = layout;
        }

        private async void BtnPickVideo_Clicked(object sender, EventArgs e)
        {
            if (!CrossMedia.Current.IsPickVideoSupported)
            {
                DisplayAlert("UYARI", "Galeriye erişme yetkiniz yok!", "OK");
                return;
            }
            var file = await CrossMedia.Current.PickVideoAsync();

            if (file == null)
                return;

            DisplayAlert("UYARI", "Seçilen video: " + file.Path, "OK");
            file.Dispose();
        }

        private async void BtnTakeVideo_Clicked(object sender, EventArgs e)
        {
            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakeVideoSupported)
            {
                DisplayAlert("UYARI", "Cihazınızın kamerası aktif değil!", "OK");
                return;
            }

            var file = await CrossMedia.Current.TakeVideoAsync(
                new Plugin.Media.Abstractions.StoreVideoOptions
                {
                    Name = DateTime.Now + ".mp4",
                    Directory = "MediaPluginPhotoVideo",
                    Quality = Plugin.Media.Abstractions.VideoQuality.High,
                    DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front
                });

            if (file == null)
                return;

            DisplayAlert("UYARI",
                "Video başarılı bir şekilde kayıt edildi: " + file.Path, "OK");

            file.Dispose();
        }

        private async void BtnPickPhoto_Clicked(object sender, System.EventArgs e)
        {
            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                DisplayAlert("UYARI", "Galeriye erişme yetkiniz yok!", "OK");
                return;
            }
            var file = await CrossMedia.Current.PickPhotoAsync();

            if (file == null)
                return;

            img.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                file.Dispose();
                return stream;
            });
        }

        private async void BtnTakePhoto_Clicked(object sender, System.EventArgs e)
        {
            int a;

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                a = 0;
                int b;
                DisplayAlert("UYARI", "Cihazınızın kamerası aktif değil!", "OK");
                b = 0;
                return;
            }

            var file = await CrossMedia.Current.TakePhotoAsync(
                new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory = "MediaPluginPhoto",
                    Name = DateTime.Now + ".jpg",
                    DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front
                });

            if (file == null)
                return;

            img.Source = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();
                file.Dispose();
                return stream;
            });
        }
    }
}

ResimYukle.axml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="hackathon.TabbedPages.ResimYukle"
             Title="Yükle">
</ContentPage>

我是通过从这里查看我的例子来做到的: https://github.com/ozaksuty/Xamarin-Ogreniyorum/tree/master/MediaPlugin

1 个答案:

答案 0 :(得分:1)

为了将来的参考,我将在评论中采用apineda的答案并详细说明。

这里的错误是您只在共享PCL项目上安装了NuGet包。您需要做的是将其安装在您的PCL项目以及您的平台项目上。

这是因为这些插件和Xamarin.Forms的工作方式。像这样的插件实际发生的是它为您提供了一种抽象的方法。 Xamarin.Forms的目标是多平台,但在一天结束时,它将转变为原生应用程序。因此,它需要在实际平台上实现。对于此示例,用于显示摄像头的代码在Android和iOS(以及所有其他平台)之间存在很大差异。

因此,实际上,您正在共享库中安装插件以获取您调用的方法,但它未实现。然后,通过为您的平台项目安装相同的插件(但它需要另一个二进制文件),该方法将实现它。

很难确定是否需要在所有项目上安装插件,或者只是共享。如果它使用任何平台特定的东西,请尝试自己决定。