1
我想知道是否存在一個來自viewmodel屬性的綁定機制,該屬性在我的特定編輯文本中提供了焦點(光標閃爍或指示textedit具有焦點)選擇。如何在mvvmcross中創建一個可編輯的edittext
我想知道是否存在一個來自viewmodel屬性的綁定機制,該屬性在我的特定編輯文本中提供了焦點(光標閃爍或指示textedit具有焦點)選擇。如何在mvvmcross中創建一個可編輯的edittext
這是一個普遍的問題MVVM - 像MVVM Focus To Textbox
正如在一般性的問題,在MvvmCross你可以在你的視圖中的一些代碼,這樣做的背後。例如,你可以創建一個輔助類,如:
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();
}
此代碼沒有測試 - 但應大致工作。
CreateBinding在活動中不存在... –