Android 中的 MVVMCross 绑定 [英] MVVMCross Bindings in Android

查看:14
本文介绍了Android 中的 MVVMCross 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在 android 中启动的跨平台应用程序.我找到了你的 MVMCross 项目,我正在尝试进入它.现在我对它完全陌生,不知道如何将我的 WebService-Results 绑定到我的 ListView.这里有一些 XAML 作为示例,我是如何尝试的:

I'm developing a cross-platform App starting it in android. I've found your MVVMCross project and I'm trying to get into it. Right now I'm totally new to it and don't know how to bind my WebService-Results to my ListView. Here a bit of XAML as example how I'm trying it:

xmlns:mobsales="http://schemas.android.com/apk/res/MobSales.DroidUI"
... 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="5dp" 
mobsales:MvxItemTemplate="@layout/listitem_customer" 
mobsales:MvxBind="{'ItemSource':{'Path':'Customer'}}" /> 
...

就是这个样子

<cirrious.mvvmcross.binding.android.views.MvxBindableListView
        android:id="@+id/autocomplete"
        android:layout_below="@id/txtfield"
        android:layout_centerHorizontal="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        mobsales:MvxItemTemplate="@layout/listitem_customer"
        mobsales:MvxBind="{'ItemSource':{'Path':'Customers'}}" />

当我将鼠标悬停在最后两行时,工具提示说未声明属性.我真的不知道你是怎么做到的.你能给我一些建议吗?我想我必须在我的 UI 项目的 Values 中写一些 xml,对吗?

When I'm hovering the last two lines the tooltip says attribute is not declared. I really don't know how you do this. Can you give me some advice? I think I have to write some xml in the Values of my UI project, right?

另一个问题:如何使用 AutoCompleteTextViews?我必须先为它编写自己的 MvXBindables 吗?有什么建议吗?:-)

Another question: How could I use AutoCompleteTextViews? Do I have to write my own MvXBindables first for it? Any advice? :-)

推荐答案

要绑定这些属性,您需要包含命名空间 - 看起来您已经完成了.

To get these attributes to bind you need to include the namespace - which it looks like you've done.

您还需要将 MvxBindingAttributes.xml 文件包含到您的 UI 项目中 - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross.Binding/ResourcesToCopy/MvxBindingAttributes.xml - 您必须设置构建操作这个文件到AndroidResource"

You also need to include the MvxBindingAttributes.xml file into your UI project - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross.Binding/ResourcesToCopy/MvxBindingAttributes.xml - and you must set the build action of this file to "AndroidResource"

例如,请参阅任何 Android 示例项目 - https://github.com/slodge/MvvmCross

For an example, see any of the Android sample projects - https://github.com/slodge/MvvmCross

对于关于添加绑定的问题的第二部分,绑定框架应该自动单向绑定(从 ViewModel 到 View)到任何 Monodroid View/widget 上的现有公共属性.

For the second part of your question about adding bindings, the binding framework should automatically one-way bind (from ViewModel to View) to existing public Properties on any Monodroid View/widget.

如果公共属性的类型不正确(例如,它是一些 Android 枚举而不是视图),那么您可以使用 IMvxValueConverter 进行转换.

If the public Property isn't of the correct type (e.g. it's some Android enumeration instead of a View), then you can use an IMvxValueConverter to do the conversion.

如果您想要进行 2 向绑定,或者没有您想要绑定的公共属性,那么您可以很容易地进行自定义绑定.有关此示例,请参阅 会议样本

If you want to do 2-way binding, or there isn't a public Property for what you want to bind to, then you fairly easily do a custom binding. For an example of this, see the custom IsFavorite 2 way binding in the conference sample

此代码为每个 Android 按钮添加了一个新的可绑定伪属性IsFavorite".

This code adds a new bindable pseudo-property "IsFavorite" to every Android Button.

... 这是在 Setup.cs 中使用如下代码初始化的:

... this is initialised in Setup.cs using code like:

    protected override void FillTargetFactories(MvvmCross.Binding.Interfaces.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);

        registry.RegisterFactory(
                    new MvxCustomBindingFactory<Button>(
                       "IsFavorite", 
                       (button) => new FavoritesButtonBinding(button)));
    }

... 绑定代码是:

public class FavoritesButtonBinding
    : MvxBaseAndroidTargetBinding
{
    private readonly Button _button;
    private bool _currentValue;

    public FavoritesButtonBinding(Button button)
    {
        _button = button;
        _button.Click += ButtonOnClick;
    }

    private void ButtonOnClick(object sender, EventArgs eventArgs)
    {
        _currentValue = !_currentValue;
        SetButtonBackground();
        FireValueChanged(_currentValue);
    }

    public override void SetValue(object value)
    {
        var boolValue = (bool)value;
        _currentValue = boolValue;
        SetButtonBackground();
    }

    private void SetButtonBackground()
    {
        if (_currentValue)
        {
            _button.SetBackgroundResource(Resource.Drawable.star_gold_selector);
        }
        else
        {
            _button.SetBackgroundResource(Resource.Drawable.star_grey_selector);
        }
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            _button.Click -= ButtonOnClick;
        }
        base.Dispose(isDisposing);
    }

    public override Type TargetType
    {
        get { return typeof(bool); }
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.TwoWay; }
    }
}

这篇关于Android 中的 MVVMCross 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆