xamarin.android - 如何为 MvxItemTemplate 创建 View 的操作监听器

标签 xamarin.android mvvmcross

我有一个包含 MvxListView 和表单的 View 。我可以在 View 代码中使用以下代码隐藏软键盘(因为这是纯粹的 View 问题)

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 的任何 View 代码,所以我可以在哪里添加代码?

最佳答案

我认为最简单的方法是在列表项中使用自定义“ View ”。

注意:这里的“ View ”指的是 Android View - 不是模型- View -ViewModel View - 抱歉命名困惑!

创建自定义 View 很容易......

只需创建一个自定义 View - 例如

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 中使用该 View ,就像使用 Android 或 Mvx View 一样:

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

如果您发现 angevelle.app1.ui.droid.controls 过于冗长,那么您可以在 setup.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=....
     />

另一种方法可能是以某种方式自定义列表...

如果您确实需要完全自定义 ListView 及其适配器,那么可以使用相同类型的技术轻松完成 - 从 UI 项目中的 MvxBindableListView 继承:

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

其中 MyListAdapter 覆盖 View 创建:

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
      ....
     />

有关自定义 View 的更多示例,请查看 GitHub - 例如https://github.com/Cheesebaron 中的一些日历、ColorPicker、ActionBar 项目

不要指望您的自定义控件能够在 xamarin 设计器中呈现(好吧,还没有......)


最后两个注释...

  1. 要重用代码...您可能希望以某种方式将 HideSoftInputFromWindow 功能放入扩展方法中,以便您只需调用 anyEditText.HideOnDone()

  2. 在 Views/UIViews 上使用 Monodroid/monotouch 事件时要小心 - 这些事件往往使用 native 委托(delegate)/监听器 - 因此有时您会发现附加某些内容来订阅一个事件可能会取消附加其他内容!一般来说,只要不将 C# 事件订阅与 native 监听器/委托(delegate)处理程序同时混合和匹配就可以了。

关于xamarin.android - 如何为 MvxItemTemplate 创建 View 的操作监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12934654/

相关文章:

c# - Xamarin.Forms - 标签 FontSize OnPlatform - XAML 错误

android - MvxAutoCompleteTextView 停留在 'Wait starting for '

listview - 如何将列表项绑定(bind)到它是否包含在另一个集合中

c# - 直接下载mp3文件

xamarin - 汉堡包菜单 Xamarin Forms (MasterDetailPage)

android - Mac 上的 debug.keystore 在哪里?

c# - MVVMCross:在 android xml 中绑定(bind)嵌套属性

c# - 在 android 的 Mono 中画一条线

android - 找不到 Mvvmcross MvxListview MvxItemTemplate 资源

ios - 用于显示 UIView 的全局钩子(Hook)