如何在片段Xamarin中执行onClickListner?

时间:2016-01-18 10:08:15

标签: android xamarin

我遇到了一些与按钮点击有关的问题

这是代码,

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.Inflate(Resource.Layout.fragment_sample, container, false);

        ImageView Send_Button = FindViewById<ImageView>(Resource.Id.HelpBtnBlack);
        Send_Button.Click += delegate 
        {

        };
    }
    public override void OnClick(View V)
    {
        //do sth...

    }

错误发生在FindViewById

2 个答案:

答案 0 :(得分:0)

获取对View

的引用
var view = inflater.Inflate(Resource.Layout.fragment_sample, container, false);

然后你可以拨打view.FindViewById<your_control_type>(Resource.Id.your_control_id)

将代码更新为此按钮:

var Send_Button = view.FindViewById<Button>(Resource.Id.HelpBtnBlack);

或对于ImageView:

var Send_Button = view.FindViewById<ImageView>(Resource.Id.HelpBtnBlack);

答案 1 :(得分:0)

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.fragment_sample, container, false);

            ImageView Send_Button = view.FindViewById<ImageView>(Resource.Id.HelpBtnBlack);
            Send_Button.Click += delegate 
            {

            };
            return view;
        }
        public override void OnClick(View V)
        {
            //do sth...

        }