NullReferenceException

NullReferenceException

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
                                
                                    (29个答案)
                                
                        
                                2年前关闭。
            
                    
我正在尝试从电话图库中添加图像,但一直获取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
            }
        };

最佳答案

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

GalleryImageService_Android.cs文件中的名称空间声明上方是否有此名称?

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

关于c# - Xamarin表单DependencyService nullReferenceException ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42874266/

10-15 03:11