我正在尝试使用Mono for Android将代码从本教程移植到 C# 。这是我的代码:
文件布局.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="fill_parent">
<TextView
android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textStyle="bold"
android:layout_marginTop="5dip"
android:layout_marginLeft="5dip" />
<TextView
android:text="@+id/TextView02"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip" />
</LinearLayout>
FileArrayAdapter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace FileChooser
{
public class FileArrayAdapter : ArrayAdapter<Option>
{
private Context c;
private int id;
private List<Option> items;
public FileArrayAdapter(Context context, int textViewResourceId,
List<Option> objects)
: base(context, textViewResourceId, objects)
{
c = context;
id = textViewResourceId;
items = objects;
}
public Option getItem(int i)
{
return items[i];
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)c.GetSystemService(Context.LayoutInflaterService);
v = vi.Inflate(id, null);
}
Option o = items[position];
if (o != null)
{
TextView t1 = (TextView)v.FindViewById(Resource.Id.TextView01);
TextView t2 = (TextView)v.FindViewById(Resource.Id.TextView02);
if (t1 != null)
t1.Text = o.getName().ToString();
if (t2 != null)
t2.Text = o.getData().ToString();
}
return v;
}
}
}
活动1.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.OS;
using Java.IO;
using Environment = Android.OS.Environment;
namespace FileChooser
{
[Activity(Label = "FileChooser", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : ListActivity
{
private File currentDir;
private FileArrayAdapter adapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
currentDir = new File(Environment.RootDirectory.AbsolutePath);
fill(currentDir);
}
private void fill(File f)
{
File[] dirs = f.ListFiles();
Title = ("Current Dir: " + f.Name);
List<Option> dir = new List<Option>();
List<Option> fls = new List<Option>();
try
{
foreach (var ff in dirs)
if (ff.IsDirectory)
dir.Add(new Option(ff.Name, "Folder", ff.AbsolutePath));
else
fls.Add(new Option(ff.Name, "File Size: " + ff.Length(), ff.AbsolutePath));
dir.AddRange(fls);
if (!String.Equals(f.Name, "sdcard", comparisonType: StringComparison.InvariantCultureIgnoreCase))
dir.Insert(0, new Option("..", "Parent Directory", f.Parent));
adapter = new FileArrayAdapter(this, Resource.Layout.FileLayout, dir);
this.ListAdapter = adapter;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Error: ", e.Message);
}
}
}
}
我收到以下错误:
The program 'Mono' has exited with code 255 (0xff).
我在最后一条语句之后放了一个断点。这是我的局部变量的截图: