如何使用 MvxBaseAndroidTargetBinding 将路径图像绑定(bind)到按钮?
1)我创建一个绑定(bind)
public class MvxButtonIconBinding: MvxBaseAndroidTargetBinding
{
private readonly View _view;
public MvxButtonIconBinding(View view)
{
_view = view;
}
public override void SetValue(object value)
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
path = Path.Combine(path, "pictures");
path = Path.Combine(path, (string)value);
if (File.Exists(path))
{
var dr = Drawable.CreateFromPath(path);
Button b = _view as Button;
var drawables = b.GetCompoundDrawables();
foreach (var d in drawables)
if (d!=null)
d.Dispose(); // To avoid "out of memory" messages
b.SetCompoundDrawablesWithIntrinsicBounds(null, dr, null, null);
}
else
{
Console.WriteLine("File {0} does not exists", path);
}
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
public override Type TargetType
{
get { return typeof(string); }
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
}
base.Dispose(isDisposing);
}
2) 设置绑定(bind):
registry.RegisterFactory(new MvxCustomBindingFactory<View>("ButtonIcon", view => new MvxButtonIconBinding(view)));
3) 创建button_list.аxml
<Button
android:id="@+id/ButtonArticle"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:gravity="bottom|center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
local:MvxBind="{'Click':{'Path':'Command1'},'ButtonIcon':{'Path':'Item.PictureFileName'}}"
android:textSize="14dip"
android:textColor="@drawable/ToggleButtonSelector" />
哪个路径传输属性PictureFileName?请给我举个例子。
最佳答案
根据您的代码,图片应位于:
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
path = Path.Combine(path, "pictures");
path = Path.Combine(path, (string)value);
因此,如果您的输入值 Item.PictureFileName
是“icon1.png”,那么您需要将图像保存在:
/data/data/YOUR_APP_NAME/files/pictures/icon1.png
我根据 Database File Location for SQLite within MonoDroid 中关于System.Environment.SpecialFolder.Personal
的信息解决了这个问题|
如果您使用的是固定图标集(不是下载或动态创建图像),那么最好使用像 the conference sample 中的 SetButtonBackground
中所示的 resourceId 方法。
关于c# - 绑定(bind)按钮路径图片(使用MvxBaseAndroidTargetBinding)Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13011901/