Is there a need for ". this" in an Android Xamarin project?

Asked

Viewed 36 times

0

I’m rewriting an application that was originally written in Java in Android Studio. Now I’m rewriting in C# for Xamarin Android, so a doubt arose me, in several classes I came across the java code like this:

private void obterWidgets()
        {
            ibtAdicionar = (ImageButton)findViewById(Resource.Id.layout_listview_ibt_adicionar);
            ibtAdicionar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Bundle parametros = new Bundle();
                parametros.putInt("RegNumPedido", pedido.getRegNumPedido());
                startActivity(new Intent(PedidoItensGradeActivity.this, MercadoriasGradeActivity.class).putExtras(parametros));
            }
});
        lvItens = (ListView) findViewById(Resource.Id.layout_listview_lv_dados);
tvEmpty = (TextView) findViewById(Resource.Id.layout_listview_tv_empty);
tvEmpty.setText("Nenhum item encontrado");
    }

Which in C# looks that way:

private void obterWidgets()
 {
            ibtAdicionar = (ImageButton)findViewById(Resource.Id.layout_listview_ibt_adicionar);
            ibtAdicionar.OnClickListener = new OnClickListenerAnonymousInnerClass(this);
        lvItens = (ListView) findViewById(Resource.Id.layout_listview_lv_dados);
tvEmpty = (TextView) findViewById(Resource.Id.layout_listview_tv_empty);
tvEmpty.Text = "Nenhum item encontrado";
 }

    private class OnClickListenerAnonymousInnerClass : View.OnClickListener
    {
        private readonly MissingClass outerInstance;

        public OnClickListenerAnonymousInnerClass(MissingClass outerInstance)
        {
            this.outerInstance = outerInstance;
        }

        public override void onClick(View v)
        {
            Bundle parametros = new Bundle();
            parametros.putInt("RegNumPedido", pedido.RegNumPedido);
            startActivity((new Intent(PedidoItensGradeActivity.this, typeof(MercadoriasGradeActivity))).putExtras(parametros));
        }
    }

If you notice the code in C# we will have the anti penultimate line:

startActivity((new Intent(PedidoItensGradeActivity.this, typeof(MercadoriasGradeActivity))).putExtras(parametros));

With that . this after the Pedidoitensgradactivity causes an error, so if I remove the . this error disappears, so wanted to know, is there anything that replaces that . this or no need to have it in my code? I am asking because the application is not yet ready to be compiled!

  • 1

    What is PedidoItensGradeActivity?

1 answer

1


No need to use the .this at the end of the class. Either you use the class name or just the this. Below is an example of how the correct way to start a Activity passing "parameters" to it.

var activity = new Intent(this, typeof(MercadoriasGradeActivity));
activity.PutExtra("RegNumPedido", pedido.RegNumPedido);
StartActivity(activity);

When it comes to finding this MercadoriasGradeActivity, do the following:

string numPedido = Intent.GetStringExtra("RegNumPedido") ?? null;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.