我是Xamarin的新手。我一直在尝试从C#for Xamarin中的特定URL设置图像到Imagebutton。我用Google搜索,无法找到示例代码或文档。如果有人为我提供了有用的文档或一些示例代码,那将会很有帮助。
先谢谢...... :)
答案 0 :(得分:2)
要从本地图像设置按钮的“默认”图像,请调用SetImage
button1 = UIButton.FromType(UIButtonType.RoundedRect);
button1.SetImage(UIImage.FromFile ("sample.png"), UIControlState.Normal);
从网址申请
button1 = FindViewById(Resource.Id.RoundedRect);
var imageBitmap = GetImageBitmapFromUrl("http://xamarin.com/resources/design/home/test.png");
button1.SetImageBitmap(imageBitmap);
private Bitmap GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}