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

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

问题描述

我正在开发一个跨平台的应用程序开始它的Andr​​oid。我发现你的MVVMCross项目,我试图进入它。现在我完全新的给它,不知道怎么我的WebService,结果绑定到我的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'}}" /> 
...



正好是这样的。

exactly looks like this

<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项目的价值一些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

有关你问题的第二部分有关添加绑定,绑定框架应自动单向绑定(从视图模型查看)在任何MonoDroid的视图/小部件现有的公共属性。

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路绑定,或有ISN T A公共财产为您要绑定到的东西,那么你很容易做一个自定义绑定。对于这样的例子,看到的发布会样本

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

这代码添加一个新的可绑定伪财产IsFavorite每一个Android的按钮。

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)));
    }



...并绑定代码为:

... and the binding code is:

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; }
    }
}

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

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