如何为 MvxItemTemplate 创建视图的动作侦听器 [英] How to create view's actions listener for MvxItemTemplate

查看:19
本文介绍了如何为 MvxItemTemplate 创建视图的动作侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a view which contains an MvxListView and a form. I can hide the softkeyboard using the following code in my view's code (as this is pure view concerns)

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

In my item template I have an other text editor and would like to have the same behavior. But where can I add my code as I don't have any view code for the MwxItemTemplate ?

推荐答案

我认为最简单的方法是在列表项中使用自定义视图".

I think the easy way to do this is to use a custom 'View' within the listitem.

注意:这里的视图"指的是 Android 视图 - 不是 Model-View-ViewModel 视图 - 抱歉命名混乱!

Note: that 'View' here refers to Android Views - not Model-View-ViewModel views - sorry for the naming confusion!

创建自定义视图很容易...

Creating custom views is easy to do...

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

Just create a custom View - e.g.

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 视图一样:

Then you can use that View in your AXML just as you do Android or Mvx views:

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

如果您发现 angevelle.app1.ui.droid.controls 过于冗长,那么您可以使用 setup.cs 中的缩写来缩短它:

If you are finding angevelle.app1.ui.droid.controls too verbose, then you could shorten this using an abbreviation in 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=....
     />

<小时>

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


An alternative approach might be to somehow customise the list...

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

If you ever do need to completely customise a listview and its adapter, then this can be easily done using the same type of technique - inherit from MvxBindableListView in your UI project:

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

其中 MyListAdapter 覆盖视图创建:

where MyListAdapter overrides the view creation:

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 但添加了您自己的自定义功能.

where MySpecialListItemView inherits from MvxBindableListItemView but adds your own custom features.

使用这种方法,您的列表将从:

Using this approach your list would then change from:

<Mvx.MvxBindableListView
      ....
     />

到:

<Abv.MyListView
      ....
     />

<小时>

有关自定义视图的更多示例,请查看 GitHub - 例如在 https://github.com/Cheesebaron

不要指望您的自定义控件会在 xamarin 设计器中呈现(嗯,还没有……)

Don't expect your custom controls to render in the xamarin designer (well, not yet...)

最后两个笔记...

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

  1. To reuse code... you might want to put that HideSoftInputFromWindow functionality in an extension method somehow so that you can just call anyEditText.HideOnDone()

在 Views/UIViews 上使用 Monodroid/monotouch 事件时要小心——这些事件往往使用原生的委托/监听器——所以有时你会发现附加一些东西来订阅一个事件可以取消附加其他东西!一般来说,只要不与本机侦听器/委托处理程序同时混合和匹配 C# 事件订阅,就可以了.

Be careful when using the Monodroid/monotouch events on Views/UIViews - these events tend to use the native delegates/listeners - and so sometimes you can find that attaching something to subscribe to one event can unattach something else! Generally you are OK as long as you don't mix and match the C# event subscriptions at the same time as the native listener/delegate handlers.

这篇关于如何为 MvxItemTemplate 创建视图的动作侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆