2012-10-17 64 views
1

我有一個包含MvxListView和窗體的視圖。我可以在視圖的代碼使用下面的代碼隱藏softkeyboard(因爲這是純粹的觀點考慮)如何爲MvxItemTemplate創建視圖的動作偵聽器

var editText = FindViewById<EditText>(Resource.Id.newCheckbookName); 
editText.EditorAction += (object sender, TextView.EditorActionEventArgs e) => 
    { 
     if (e.ActionId == ImeAction.Done) 
     { 
      InputMethodManager inputMgr = GetSystemService(InputMethodService) as InputMethodManager; 
      inputMgr.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0); 
     } 
     return; 
    }; 

在我的項目模板,我有一個其他文本編輯器,並希望有相同的行爲。但是我可以在哪裏添加我的代碼,因爲我沒有任何關於MwxItemTemplate的視圖代碼?

回答

2

我認爲簡單的做法是在列表項中使用自定義的「查看」。

注意:這裏'視圖'是指Android視圖 - 而不是模型 - 視圖 - 視圖模型的意見 - 抱歉的命名混淆!

創建自定義視圖是很容易做到......

只需創建一個自定義視圖 - 例如

namespace Angevelle.App1.UI.Droid.Controls 
{ 
    public class MyText : EditText 
    { 
     public MyText(Context context, IAttributeSet attrs) 
      : base(context, attrs) 
     { 
      this.EditorAction += OnEditorAction; 
     } 

     private void OnEditorAction(object sender, EditorActionEventArgs editorActionEventArgs) 
     { 
      if (editorActionEventArgs.ActionId == ImeAction.Done) 
      { 
       // this code not tested - but something like this should work 
       var imm = (InputMethodManager)Context.GetSystemService(Context.InputMethodService); 
       imm.HideSoftInputFromWindow(WindowToken, 0); 
      } 
     } 
    } 
} 

然後你就可以使用查看在您的AXML就像你做Android或MVX觀點:

<angevelle.app1.ui.droid.controls.MyText 
     android:layout_height=.... 
    /> 

如果您發現angevelle.app1.ui.droid.controls太冗長,那麼你可以這樣使用設定的縮寫縮短的.cs:

protected override IDictionary<string, string> ViewNamespaceAbbreviations 
    { 
     get 
     { 
      var abbreviations = base.ViewNamespaceAbbreviations; 
      abbreviations["Abv"] = "angevelle.app1.ui.droid.controls"; 
      return abbreviations; 
     } 
    } 

那麼你可以使用:

<Abv.MyText 
     android:layout_height=.... 
    /> 

另一種方法可能會以某種方式自定義列表...

如果你確實需要完全自主的列表視圖和它的適配器,那麼可以很容易地使用相同類型的技術做 - 從MvxBindableListView在UI項目繼承:

public class MyListView : MvxBindableListView 
{ 
     public MyListView(Context context, IAttributeSet attrs); 
      : base(context, attrs, new MyListAdapter(context)) 
     { 
     } 
} 

其中MyListAdapter覆蓋視圖創建:

public class MyListAdapter : MvxBindableListAdapter 
{ 
    public MyListAdapter(Context context) 
     : base(context) 
    { 
    } 

    // put your custom Adapter code here... e.g.: 
    protected override MvxBindableListItemView CreateBindableView(object source, int templateId) 
    { 
     return new MySpecialListItemView(_context, _bindingActivity, templateId, source); 
    } 
} 

其中MySpecialListItemView從MvxBindableListItemView繼承,但添加了您自己的自定義功能。

使用這種方法,那麼你的列表會改變來自:

<Mvx.MvxBindableListView 
     .... 
    /> 

到:

<Abv.MyListView 
     .... 
    /> 

有關自定義視圖的更多例子,看看周圍的GitHub看看 - 例如在https://github.com/Cheesebaron

一些日曆,顏色拾取,動作條的項目不要指望你的自定義控件在xamarin設計師渲染(當然,沒有...)


最後兩個音符.. 。

  1. 重用代碼...你可能想要把那HideSoftInputFromWindow功能的擴展方法在某種程度上,這樣你可以叫anyEditText.HideOnDone()

  2. 使用MonoDroid的時候/ MonoTouch的事件的次數要小心/ UIViews - 這些事件傾向於使用本地代理/監聽器 - 所以有時候你會發現附加一些事件來訂閱一個事件可以取消其他的事情!一般而言,只要不與本地偵聽器/代理處理程序同時混合和匹配C#事件訂閱,就可以。

+0

非常感謝!完美的答覆是平常的。我發現並使用ViewNamespaceAbbreviations已經;-) –

相關問題