Xamarin.Android自定义组件使用xaml单击事件绑定

时间:2018-04-05 07:57:34

标签: xamarin.android event-handling custom-controls

我正在创建一个包含标签和按钮的自定义按钮组件。在attrs.xml中定义后,我可以使用XAML设置displaytext和其他属性(比如

<UButton displayText="Hello" ... />

)。但是我需要允许用户通过在XAML中定义它来处理此控件的Click事件,如

    mclick="button_click"

。但我无法找到处理此问题的文件。你能指导我吗?

1 个答案:

答案 0 :(得分:0)

让我们看一下Button的源代码。

在此link(Android attrs.xml源代码)中,搜索onClick,您会发现:

<!-- Name of the method in this View's context to invoke when the view is
     clicked. This name must correspond to a public method that takes
     exactly one parameter of type View. For instance, if you specify
     <code>android:onClick="sayHello"</code>, you must declare a
     <code>public void sayHello(View v)</code> method of your context
     (typically, your Activity). -->
<attr name="onClick" format="string" />

这就是你使用时的原因:

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:onClick="onClick"/>

它会找到onClick方法。

Here,您会看到Android如何处理android:onClick="onClick"View(Context context, AttributeSet attrs, int defStyleAttr)方法,android:onClick="onClick"对应R.styleable.View_onClick:< / p>

 case R.styleable.View_onClick:
            if (context.isRestricted()) {
                throw new IllegalStateException("The android:onClick attribute cannot "
                        + "be used within a restricted context");
            }

            final String handlerName = a.getString(attr);
            if (handlerName != null) {
                setOnClickListener(new OnClickListener() {
                    private Method mHandler;

                    public void onClick(View v) {
                        if (mHandler == null) {
                            try {
                                mHandler = getContext().getClass().getMethod(handlerName,
                                        View.class);
                            } catch (NoSuchMethodException e) {
                                int id = getId();
                                String idText = id == NO_ID ? "" : " with id '"
                                        + getContext().getResources().getResourceEntryName(
                                            id) + "'";
                                throw new IllegalStateException("Could not find a method " +
                                        handlerName + "(View) in the activity "
                                        + getContext().getClass() + " for onClick handler"
                                        + " on view " + View.this.getClass() + idText, e);
                            }
                        }

                        try {
                            mHandler.invoke(getContext(), View.this);
                        } catch (IllegalAccessException e) {
                            throw new IllegalStateException("Could not execute non "
                                    + "public method of the activity", e);
                        } catch (InvocationTargetException e) {
                            throw new IllegalStateException("Could not execute "
                                    + "method of the activity", e);
                        }
                    }
                });
            }
            break;

你会发现,它通过反思找到了方法。

所以,我猜你忘了处理mclick="button_click"了。通常,我们不会使用它来添加点击监听器,这有点复杂。我们使用View&#39; s setOnClickListener

相关问题