c# - 绑定(bind)按钮路径图片(使用MvxBaseAndroidTargetBinding)Android

标签 c# mono xamarin.android mvvmcross

如何使用 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?请给我举个例子。


Taken from

最佳答案

根据您的代码,图片应位于:

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/

相关文章:

mono - 无法打开 MonoDevelop v 2.4.2

xamarin.forms - Xamarin Forms Android 中步进器宽度无法正确调整大小

xamarin - xamarin.android 中的 Xam.Plugin.Media 预先获取目标文件夹

c# - Xamarin - 按 "Add NuGet Packages..."没有任何作用

c# - 保存和加载图像 SQLite C#

c# - ASP.net 中的列表框工具提示问题

c# - 在 C# 中将 JS 'escape' d 字符串转换回正确的格式

java - 简单的跨平台 GUI 应用程序

apache - 如何解决 Failed running '/usr/lib/pkgconfig/../../bin/mod-mono-server2 --filename/tmp/mod_mono_server_global --nonstop --master

Android Xamarin - 使用图片库选择器时应用程序关闭