MvvmCross Touch 项目中的自定义可绑定控件 [英] Custom bindable control in a MvvmCross Touch project

查看:22
本文介绍了MvvmCross Touch 项目中的自定义可绑定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MvxBaseBindableCollectionViewCell,它加载一个包含自定义按钮的 xib.我希望能够将此自定义按钮传递给要绑定到的 ViewModel.这可能吗?

I have a MvxBaseBindableCollectionViewCell which loads a xib that contains a custom button. I would like to be able to pass this custom button a ViewModel to bind to. Is this possible?

我正在尝试实现类似 MyButton.ViewModel = ViewModel.ChildViewModel 的内容,并将 ViewModel.ChildViewModel.Name 显示为按钮标题.

I'm trying to acheive something like MyButton.ViewModel = ViewModel.ChildViewModel and have ViewModel.ChildViewModel.Name show as the button title.

推荐答案

如果您想自定义绑定单元格,那么 http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html

If you want to custom bind a cell, then there's a tutorial on this in http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html

如果你想在那个视图中创建一个完全可绑定的 UIButton 那么你可以使用一些继承来做到这一点:

If you want to create a fully bindable UIButton within that View then you can do this using some inheritance like:

[Register("MyButton")]
public class MyButton
    : UIButton
      , IMvxServiceConsumer
{
    private IList<IMvxUpdateableBinding> _bindings;

    private const string BindingText = "SpecialTitle Customer.Name";

    public MyButton()
    {
    }

    public MyButton(IntPtr handle)
        : base(handle)
    {
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            foreach (var binding in _bindings)
            {
                binding.Dispose();
            }
            _bindings.Clear();
        }
        base.Dispose(disposing);
    }

    private object _dc;

    public object DataContext
    {
        get { return _dc; }
        set
        {
            _dc = value;
            if (_bindings == null)
            {
                var binder = this.GetService<IMvxBinder>();
                _bindings = binder.Bind(_dc, this, BindingText).ToList();
            }
            else
            {
                foreach (var binding in _bindings)
                {
                    binding.DataContext = _dc;
                }
            }
        }
    }

    public string SpecialTitle
    {
        get { return this.GetTitle(UIControlState.Normal); }
        set { this.SetTitle(value, UIControlState.Normal); }
    }
}

<小时>

旁白> MvvmCross v3Hot Tuna"将包含一些帮助类,以使其更简单.


Aside> MvvmCross v3 "Hot Tuna" will contain some helper classes to make this a bit simpler to do.

这篇关于MvvmCross Touch 项目中的自定义可绑定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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