简体   繁体   中英

RegisterDataSetObserver in C# Xamarin

What is the C# equivalent of this Java code:

ChatArrayAdapter chatArrayAdapter;

chatArrayAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
    super.onChanged();
    listView.setSelection(chatArrayAdapter.getCount() - 1);
}});

I got this from here, Android Chat Bubble and I am trying to convert Java into C# (Xamarin).

This should get you started.

[Activity(Label = "App4", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    int count = 1;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

        MyAdapter myAdapter = new MyAdapter(this, 0) ;

        myAdapter.RegisterDataSetObserver(new MyDataSetObserver());
    }
}

public class MyAdapter : ArrayAdapter
{
    public MyAdapter(Context context, int layout) : base (context, layout)
    {

    }
}

public class MyDataSetObserver : DataSetObserver
{
    public override void OnChanged()
    {
        base.OnChanged();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM