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

查看:68
本文介绍了如何为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);
            }
        }
    }
}

然后,您可以像在Android或Mvx视图中一样在AXML中使用该视图:

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

两个最后的笔记...

Two final notes...

  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天全站免登陆