错误:'Spi​​nner'不包含getSelectedItem'...'的定义

时间:2017-07-28 02:33:00

标签: android xamarin android-spinner

我需要从我的微调器中取出所选项目,但我收到上述错误,我不知道为什么。

所以,我希望在不调用微调器的事件处理程序的情况下获取微调器选定的项目,如果可能的话,因为我需要在我的按钮单击上使用它。

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

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

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

         //
         //getSelectedItem does not contain a definition!!!
         //
        String spinner_text = spinner.getSelectedItem().toString();

         //
         //Do I need this? 
         //
        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);


        var adapter = ArrayAdapter.CreateFromResource(
                this, Resource.Array.planet_array, global::Android.Resource.Layout.SimpleSpinnerItem);
        adapter.SetDropDownViewResource(global::Android.Resource.Layout.SimpleSpinnerDropDownItem);
        spinner.Adapter = adapter;

        bt_ok.Click += delegate
        {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.SetTitle("Confirm");

            //
            //I need the value here!!!
            //
            builder.SetMessage(new Confirm().Choice(spinner_text));


            builder.SetCancelable(true);
            builder.SetPositiveButton("Yes", delegate { });
            builder.Show();
        };
    }

我尝试了thisthis个答案,但我无法让它发挥作用。

1 个答案:

答案 0 :(得分:2)

getSelectedItem中的Java Xamarin.Android方法转换为C#只读属性(SelectedItem):

var spinner = FindViewById<Spinner>(Resource.Id.dd_spinner);
var selectedItem = spinner.SelectedItem;