简体   繁体   中英

Xamarin Forms and xam.plugin.media: could not resolve reference to Xamarin.Essentials.Permissions

I am following a tutorial on how to browse images on the phone with Xamarin Forms. In the MainPage.xaml I have:

        <Button Text="Select a picture"
                Clicked="SelectImageButton_Clicked"/>
        <Image x:Name="selectedImage"/>
    </StackLayout>

In the MainPage.xaml.cs, is the following code for the Click event handler:

private async void SelectImageButton_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();
            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("Error", "This is not supported on your device", "OK");
                return;
            }

            var mediaOptions = new PickMediaOptions()
            {
                PhotoSize = PhotoSize.Medium
            };

            var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaOptions);

            if (selectedImageFile == null)
            {
                await DisplayAlert("Error", "The picture is null", "OK");
                return;
            }
                selectedImage.Source = ImageSource.FromStream(() => selectedImageFile.GetStream());

        }

Building the solution works, but as soon as I press the button in the emulator, I get an error. So, I have set the linker ( Droid Properties > Android Options > Linking ) to Sdk Assemblies Only.

Then i get the following error when trying to build the solution:

Java.Interop.Tools.Diagnostics.XamarinAndroidException: error XA2006: Could not resolve reference to 'Xamarin.Essentials.Permissions/BasePlatformPermission' (defined in assembly 'Plugin.Media, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null') with scope 'Xamarin.Essentials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. When the scope is different from the defining assembly, it usually means that the type is forwarded.

I have created a new project, updated the Xamarin.Forms NuGet Package to version 4.6.0.726, updated the Xamarin.Essentials version to 1.5.3.1 and then installed the xam.plugin.media package, version 5.0.1

When trying to find similar issues with solutions, I found that there might be a reference to Essentials that is not pointing to the correct version and that I should change that in the Android project within the solution. However, since I am pretty new to Xamarin and Android development, I would not know where to find these references. Am I on the right track towards a solution, and if yes: what steps do I need to take to resolve this?

If (!rightTrack)
{
return solution;
}

Many thanks in advance for your input!

When I test your code with the same version of Xamarin.Forms, Xamarin.Essentials, xam.plugin.media, I reproduce the error BasePlatformPermission.

You could use the older version 4.0.1.5 of xam.plugin.media. The error would fix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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