简体   繁体   English

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

[英]XamariniForms, take photo, An unhandled exception occured

I would like to take one photo at Xamarin.Forms. 我想在Xamarin.Forms上合影。 But when I build it, when I click on the "Take Photo" button I get the above error. 但是,当我构建它时,当我单击“拍照”按钮时,出现了以上错误。 I put a breakpoint on all lines, but I could not find my fault. 我在所有行上都设置了断点,但是找不到我的错。

在此处输入图片说明

Click Take Photo 点击拍照 在此处输入图片说明

ResimYukle.axml.cs 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 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>

I do this by looking at my example from here : https://github.com/ozaksuty/Xamarin-Ogreniyorum/tree/master/MediaPlugin 我通过从这里查看我的示例来做到这一点: https : //github.com/ozaksuty/Xamarin-Ogreniyorum/tree/master/MediaPlugin

For future reference I will take the answer of apineda in the comments and elaborate a bit. 为了将来参考,我将在评论中使用apineda的答案并进行详细说明。

The error here is that you have installed the NuGet package only on your shared PCL project. 这里的错误是您仅在共享的PCL项目上安装了NuGet软件包。 What you need to do is install it on your PCL project as well as your platform projects . 您需要做的是将其安装在PCL项目以及平台项目中

This is because of the way these plugins and Xamarin.Forms work. 这是因为这些插件和Xamarin.Forms的工作方式。 What actually happens with plugins like these is it offers you an abstract method to work with. 此类插件实际发生的事情是它为您提供了一种抽象的工作方法。 Xamarin.Forms is targeting multi-platform, but at the end of the day, it will just transform into a native app. Xamarin.Forms的目标是跨平台的,但是最终,它将变成一个本机应用程序。 Because of that, it needs an implementation on the actual platform. 因此,它需要在实际平台上实现。 For this example, code for showing the camera differs greatly between Android and iOS (and all other platforms for that matter). 对于此示例,用于显示相机的代码在Android和iOS(以及与此相关的所有其他平台)之间存在很大差异。

So, effectively, you are installing the plugin on your shared library to get the method you call upon, but it is not implemented. 因此,实际上,您是在共享库上安装插件以获取您调用的方法,但未实现。 By then installing the same plugin (but it takes another binary) to your platform project(s), the method will get it's implementation. 然后,将相同的插件(但需要另一个二进制文件)安装到平台项目中,该方法将获取其实现。

It is kind of hard to determine whether a plugin needs to be installed on all projects, or just the shared. 很难确定是需要在所有项目中还是仅在共享项目中安装插件。 Try to decide for yourself if it uses any platform specific stuff. 尝试自己决定它是否使用任何平台特定的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM