1

我想知道是否存在来自 viewmodel 属性的绑定机制,该机制在我选择的特定编辑文本中提供焦点(闪烁的光标或指示 textedit 具有焦点的东西)。

4

1 回答 1

2

这是一个通用的 Mvvm 问题 - 比如MVVM Focus To Textbox

就像一般问题一样,在 MvvmCross 中,您可以在 View 后面的一些代码中执行此操作。例如,您可以创建一个辅助类,如:

    public class Helper
    {
        private Activity _a;

        public Helper(Activity a)
        {
            _a = a;
        }

        // TODO - this should probably be a ViewModel-specific enum rather than a string
        private string _focussedName;
        public string FocussedName
        {
            get { return _focussedName; }
            set 
            { 
                _focussedName = value;
                var mapped = MapFocussedNameToControlName(_focussedName);
                var res = _a.Resources.GetIdentifier(mapped, "id", _a.PackageName);
                var view = _a.FindViewById(res); 
                view.RequestFocus();
            }
        }

        private string MapFocussedNameToControlName(string value)
        {
            // TODO - your mapping here...
            return value;
        }
    }

然后可以将其绑定在View和中OnCreate

    private Helper _helper;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.main);

        _helper = new Helper(this);
        this.CreateBinding(_helper)
                    .For(h => h.FocussedName)
                    .To<MyViewModel>(x => x.FocusName)
                    .OneWay()
                    .Apply();
    }

此代码未经测试 - 但应该大致可以工作。

于 2013-10-23T08:58:40.723 回答