c# - Xamarin Forms DependencyService nullReferenceException

标签 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
            }
        };

最佳答案

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

GalleryImageService_Android.cs 文件中的命名空间声明上方有这个吗?

[程序集:Xamarin.Forms.Dependency(typeof(GalleryImageService_Android))]

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

相关文章:

c# - Azure WebJobs (3.x) 连续作业未在仪表板中显示函数

c# - 无法在 UWP 中调试 .NET Standard 2.0 DLL

xcode - 更新 VS 2019 后,Xamarin.ios 应用 native 链接失败

c# - FormattedString 中的 Span-Elements 似乎被修剪了

c# - 为 .NET 开发人员学习 Python

c# - 在具有外部Visual Studio版本的Windows(2017)上,无法为PC Standalone生成Unity5项目

c# - Xamarin.forms 共享项目在创建时出错

listview - 如何在 ListView 中隐藏滚动条

C#:如何判断 EXE 是否有图标?

c# - 如何使用基构造函数实例化继承类?