Now the example of BindableGridView is on https://github.com/slodge/MvvmCross/issues/37
Here is the solution we used to bind images on Buttons when the name of the file comes from a viewmodel:
1) Create a binding
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) Setup the binding :
registry.RegisterFactory(new MvxCustomBindingFactory<View>("ButtonIcon", view => new MvxButtonIconBinding(view)));
3) Use it in your gridview/listview:
<ShopBazarAndroid.MvvmCross.MvxBindableGridView
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="fill_parent"
android:id="@+id/ArticleLayout"
android:columnWidth="170dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
local:MvxItemTemplate="@layout/article_buttonlayout"
local:MvxBind="{'ItemsSource':{'Path':'VisualArticles'}}" />
"article_buttonlayout" :
<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" />
Where "Item" is my object, and "PictureFileName", the filename on the local filesystem.
I'm new on C#, be indulgent if you find errors :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…