gpt4 book ai didi

c# - Android Spinner 教程中无法隐式转换类型错误

转载 作者:行者123 更新时间:2023-11-29 22:04:26 28 4
gpt4 key购买 nike

您好,我正在尝试为 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 ();
}
}
}

最佳答案

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

spinner.ItemSelected += spinner_ItemSelected; 

...

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

(现在您只需要一个用于 Android.Widget 的 using 指令。)

关于c# - Android Spinner 教程中无法隐式转换类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126979/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com