【发布时间】:2020-09-05 03:09:54
【问题描述】:
我正在使用 Xamarin Forms 开发 Android 应用程序。我在表单上添加了 ImageCircle 插件以显示个人资料图像。我想用手机相机拍摄的照片对其进行更新。所以要做到这一点,我有这些代码。
1。 XAML
<controls:CircleImage x:Name="ImgProfile" BorderColor="DarkSlateGray" BorderThickness="5" Aspect="AspectFit" Scale="0.6" HeightRequest="150" WidthRequest="150" />
<ImageButton Source="pan.png" BackgroundColor="Transparent" Clicked="ImageButton_Clicked"></ImageButton>
2。 C#
async void TakePhoto()
{
await CrossMedia.Current.Initialize();
var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Small,
Name = Guid.NewGuid().ToString().Substring(0,8),
Directory= "profile"
});
if(file==null)
{
return;
}
ImgProfile.Source= ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}
private void ImageButton_Clicked(object sender, EventArgs e)
{
TakePhoto();
}
我也尝试了以下方法,但没有成功:
设置源 = file.Path
使用字节数组
- xaml 中的 {Binding ImageSource} 并在后面的代码中设置 imagesource。
上面的代码运行良好,我可以在监视窗口中看到字节数组/流。但是,图像仍然没有显示。
请注意:
我的方法是老式的 WinForm 方式而不是 MVVM。
插件/模块版本
VS 2019 社区 16.5.5
Xamarin Android SDK - 10.2.0.100
Xamarin.Forms 4.6.0.800
- Xamarin.Plugin.Media 5.0.1
- Xamarin.Plugins.Forms.ImageCircle 3.0.0.5
提前致谢
【问题讨论】:
-
能否在github上分享一个简单的示例,我将下载您的示例进行测试。
-
如果是普通的
Image会显示吗? -
@Morse - 是的,硬编码的图像会显示出来。
-
@CherryBu-MSFT - 对不起,我不能。它是专有的,因此不能公开共享整个项目/代码。
-
ImageCircle 只是将现有图像转换为圆形。它没有那么灵活。尝试改用 FFloading Circle-ImageTransformations
标签: c# visual-studio xamarin xamarin.forms visual-studio-2019