【发布时间】:2020-05-07 13:42:40
【问题描述】:
我正在关注如何使用 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 时,错误已修复。请试一试。 -
嘿温迪,确实成功了!如果您将其作为答案提交,那么我会将其投票为正确答案。非常感谢您花时间解决这个问题!
-
我已经在答案中发布了。不要忘记接受它。谢谢~
标签: c# android xamarin xamarin.forms