我发现许多类似的问题,我见过的大多数是Java,问题是我不知道如何将Java代码转换为C#Xamarin。
以下是我找到的代码:from here
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testCtrl">
<div >
<form name="form">
<input type="text" name="name" ng-model = "data.name" required>
<input type="text" name="age" ng-model = "data.age" required>
<input type="button" value="reset" ng-click="reset()">
</form>
</div>
</div>
我特别提到这一行:
month.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
List<String> s = Arrays.asList(getResources().getStringArray(R.array.item_day));
if (pos == 0 || pos == 2 || pos == 4 || pos == 8 || pos == 9
|| pos == 11) {
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
} else if (pos == 1) {
s = s.subList(0,28);
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
} else {
s = s.subList(0,30);
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
它的C#对应物是什么?
到目前为止,这是我的代码:
List<String> s = Arrays.asList(getResources().getStringArray(R.array.item_day));
字符串资源:
spinner1 = FindViewById<Spinner>(Resource.Id.spinner1);
spinner2 = FindViewById<Spinner>(Resource.Id.spinner2);
var adapter1 = ArrayAdapter.CreateFromResource(
this, Resource.Array.building_array, Android.Resource.Layout.SimpleSpinnerItem);
var adapter2 = ArrayAdapter.CreateFromResource(
this, Resource.Array.level_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter1.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
adapter2.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner1.Adapter = adapter1;
spinner2.Adapter = adapter2;
spinner1.ItemClick += (sender, e) => {
// code here
};
答案 0 :(得分:1)
C#对应物是
var arr = Resources.GetStringArray(Resource.Array.building_array);
这在Android上下文中有效,例如您的活动,因为Resources
是其中的属性。
如果您希望它为List<sting>
,只需在呼叫中添加.ToList()
(需要:using System.Linq;
)。