2

How to binding path image to button, using MvxBaseAndroidTargetBinding?

1) I 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) Create 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" />

Which path transmit properties PictureFileName? Show me an example please.


Taken from

1 Answer 1

0

From your code, the image should be in:

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
path = Path.Combine(path, "pictures");
path = Path.Combine(path, (string)value);

So if your input value Item.PictureFileName is 'icon1.png' then you need to have an image saved in:

/data/data/YOUR_APP_NAME/files/pictures/icon1.png

I worked this out according to the info on System.Environment.SpecialFolder.Personal in Database File Location for SQLite within MonoDroid


If you are using a fixed icon set (not downloading or dynamically creating images), then you might be better off using a resourceId approach like that shown in SetButtonBackground in the conference sample

Sign up to request clarification or add additional context in comments.

3 Comments

If this explanation doesn't work, then it might be an idea to post a sample project - e.g. to a repository on GitHub. Others might be able to help debug your sample project - but they might not have time to write a sample project to assist.
I think can be used FavoritesButtonBinding. But I do not know how to return ID relative to that name pictures
Sorry - I don't think I can help at this question level - I often don't understand what you ask. Maybe try building at test project and posting it on Github - we can both speak code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.