我希望能够在代码中而不是在xaml文件中编辑图像源的大小,以便在图像必须为空时可以处理显示。
using Pokedex.Src.Model.Pokemon.Type;
using System;
using System.Globalization;
using Xamarin.Forms;
namespace Pokedex.Src.ValueConverters
{
class TypeToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (value.GetType().Equals(typeof(TYPE)) && (TYPE) value != TYPE.UNKNOWN)
return GetTypeImageResource((TYPE) value);
return null;
}
private ImageSource GetTypeImageResource(TYPE type) {
ImageSource imageSource = ImageSource.FromResource($"Pokedex.Resources.Types.{type.ToString()}.png");
// HERE EDIT IMAGE SOURCE SIZE
return imageSource;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
}
我尝试使用BindableProperty.Create,但是即使代码中发生了某些事情,它也不会更改屏幕上的图像大小
private ImageSource GetTypeImageResource(TYPE type) {
ImageSource imageSource = ImageSource.FromResource($"Pokedex.Resources.Types.{type.ToString()}.png");
BindableProperty bp = BindableProperty.Create("WidthRequest", typeof(int), typeof(ImageSource));
var wr = imageSource.GetValue(bp); // = 0
imageSource.SetValue(bp, 48);
var wr2 = imageSource.GetValue(bp); // = 48
return imageSource;
}