2012-10-17 66 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已经;-) –

相关问题