文件夹中的 Xamarin 图像网格



我正在尝试显示带有文本的图像网格。使用文件夹中的图像。我尝试了各种方法和教程,但没有成功。我同时具有内部和外部存储的读取和写入权限。每当我调试应用程序时,我得到的只是一个空白屏幕。我尝试在适配器 GetView(( 方法上设置一个永远不会命中的断点。

这是我的 MainActivity OnCreate(( 方法:

protected override void OnCreate(Bundle bundle)
{
    if (!_dir.Exists()) { _dir.Mkdir(); }
    base.OnCreate(bundle);
    SetContentView(R.Layout.Main);
    string[] FilePath = null;
    string[] FileName = null;
    if (_dir.IsDirectory)
    {
        DroidIO.File[] imageList = _dir.ListFiles();
        FilePath = new string[imageList.Length];
        FileName = new string[imageList.Length];
        for (int i = 0; i < imageList.Length; i++)
        {
            FilePath[i] = imageList[i].AbsolutePath;
            FileName[i] = imageList[i].Name;
        }
    }
    var gvImageList = FindViewById<GridView>(R.Id.gvImageList);
    gvImageList.SetAdapter(new ImagesFromFolderAdapter(this, FilePath, FileName));
    gvImageList.ItemClick += delegate { gvImageList_ItemClick(); };
}

和我的图像适配器类:

public class ImagesFromFolderAdapter : BaseAdapter
{
    private Activity context;
    private string[] filePath;
    private string[] fileName;
    public  ImagesFromFolderAdapter(Activity c, string[] fp, string[] fn)
    {
        this.context = c;
        this.fileName = fn;
        this.filePath = fp;
    }
    public override int Count { get; }
    public int GetCount()
    {
        return filePath.Length;
    }
    public override Java.Lang.Object GetItem(int position)
    {
        return position;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
            view = context.LayoutInflater.Inflate(R.Layout.gallery_item_layout, null);
        view.FindViewById<ImageView>(R.Id.gitemImage).SetImageBitmap(BitmapFactory.DecodeFile(filePath[position]));
        view.FindViewById<TextView>(R.Id.gitemText).Text = fileName[position];
        return view;
    }
}

有什么想法我哪里出错了吗?如果有帮助,我目前正在使用 LG G3 作为调试设备。提前感谢您的任何帮助。

您为网格适配器返回了零计数。

改变

public override int Count { get; }

 public override int Count
 {
      get { return filePath.Length; }
 }

最新更新