2013-10-22 26 views

回答

2

這是一個普遍的問題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(); 
    } 

此代碼沒有測試 - 但應大致工作。

+0

CreateBinding在活動中不存在... –

相關問題