Xamarin Forms和xam.plugin.media:无法解析对Xamarin.Essentials.Permi



我正在学习如何使用Xamarin Forms在手机上浏览图像的教程。在MainPage.xaml中,我有:

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

在MainPage.xaml.cs中,是Click事件处理程序的以下代码:

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());
}

构建解决方案是可行的,但我一按下模拟器中的按钮,就会出现错误。因此,我已将链接器(Droid Properties > Android Options > Linking(设置为"仅Sdk程序集"。

然后我在尝试构建解决方案时出现以下错误:

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.

我创建了一个新项目,将Xamarin.Forms NuGet包更新到4.6.0.726版本,将Xamarin.Essentials版本更新为1.5.3.1,然后安装xam.plugin.media包,版本5.0.1

当我试图在解决方案中找到类似的问题时,我发现可能有一个对Essentials的引用没有指向正确的版本,我应该在解决方案的Android项目中更改它。然而,由于我对Xamarin和Android开发还很陌生,我不知道在哪里可以找到这些参考资料。我是否走上了解决方案的正确轨道?如果是的话:我需要采取什么步骤来解决这个问题?

If (!rightTrack)
{
return solution;
}

非常感谢您的意见!

当我使用相同版本的Xamarin.Forms、Xamarin.Essentials、xam.plugin.media测试您的代码时,我会重现错误BasePlatformPermission。

您可以使用旧版本的xam.plugin.media 4.0.1.5。该错误将得到修复。

最新更新