1

嗨,我正在尝试为 Android 做 Spinner 教程。 http://docs.xamarin.com/android/tutorials/User_Interface/spinner 我收到错误: Cannot implicitly convert type 'System.EventHandler<Android.Widget.ItemEventArgs>' to 'System.EventHandler<Android.Widget.AdapterView.ItemSelectedEventArgs>' (CS0029) 在我的 Activity1.cs 的第 26 行。我刚刚从教程中复制了代码,所以我不确定我需要将这一行更改为什么以便我可以运行它。这是我的 Activity1.cs :

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace HelloSpinner
{
[Activity (Label = "HelloSpinner", MainLauncher = true)]
public class Activity1 : 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);

        Spinner spinner = FindViewById<Spinner> (Resource.Id.spinner);

        spinner.ItemSelected += new EventHandler<ItemEventArgs> (spinner_ItemSelected); 
        var adapter = ArrayAdapter.CreateFromResource (
            this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);

        adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
        spinner.Adapter = adapter;
    }

    private void spinner_ItemSelected (object sender, ItemEventArgs e)
    {
        Spinner spinner = (Spinner)sender;

        string toast = string.Format ("The planet is {0}", spinner.GetItemAtPosition (e.Position));
        Toast.MakeText (this, toast, ToastLength.Long).Show ();
    }
}
}
4

1 回答 1

2

最简单的方法可能是只使用事件声明的委托类型......理想情况下使用更简单的方法组转换语法:

spinner.ItemSelected += spinner_ItemSelected; 

...

private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)

(现在你只需要一个 using 指令Android.Widget。)

于 2012-06-20T19:45:40.217 回答