Xamarin Forms DependencyService nullReferenceException

时间:2017-03-18 12:12:46

标签: c# xamarin.forms

我正在尝试从我的手机库中添加图片,但继续获取System.Reference,我想在按下按钮时从手机库中上传图片,并在DependencyService上提供nullReferenceException。

我的页面:

InitializeComponent();


        Device.OnPlatform(
            Android: () => {
                new Thickness(0, 20, 0, 0);
                }
        );

        Image image = new Image();

        Button addImage = new Button { Text = "Select Image" };
        addImage.Clicked += (sender, args) =>
        {
            IGalleryImageService galleryService = DependencyService.Get<IGalleryImageService>();
            galleryService.ImageSelected += (o, imageSourceEventArgs) => image.Source = imageSourceEventArgs.ImageSource;
            galleryService.SelectImage();

        };

        this.Content = new StackLayout
        {
            Children =
            {
                image,
                addImage
            }
        };

1 个答案:

答案 0 :(得分:3)

您可能忘记通过添加正确的属性来导出实现。

您是否在GalleryImageService_Android.cs文件中的命名空间声明上方有这个?

[assembly: Xamarin.Forms.Dependency (typeof (GalleryImageService_Android))]