Loop Through List

Yusuf 791 Reputation points
2022-07-11T02:57:32.223+00:00

Hi
I am try to loop through generic list, I get different errors

This works with static text

namespace MauiApp  
{  
    public class GraphicsDrawable : IDrawable  
    {  
        public void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            for (var i = 0; i < 3; i++)  
            {  
                canvas.DrawString("Static Text".ToString(), 50, 50 + 20 * i, HorizontalAlignment.Center);  
            }  
        }  
    }  
}  

This gives the runtime error when using Async and await

using MauiApp.Helper;  
  
namespace MauiApp  
{  
    public class GraphicsDrawable : IDrawable  
    {  
        FirebaseHelper firebaseHelper = new FirebaseHelper();  
  
        public long DepartmentId { get; set; }  
        public async void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            var employee = await firebaseHelper.GetDepartmentEmployees(DepartmentId); // System.Collections.Generic.List<>  
            for (var i = 0; i < employee.Count; i++)  
            {  
                canvas.DrawString(employee[i].FirstName.ToString(), 50, 50 + 20 * i, HorizontalAlignment.Center); //System.ObjectDisposedException: 'The object has been closed. (0x80000013)'  
  
            }  
        }  
    }  
}  

compile error when not using Async and await,

using MauiApp.Helper;  
namespace MauiApp  
{  
    public class GraphicsDrawable : IDrawable  
    {  
        FirebaseHelper firebaseHelper = new FirebaseHelper();  
  
        public long DepartmentId { get; set; }  
        public  void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            var employee =  firebaseHelper.GetDepartmentEmployees(DepartmentId); // System.Collections.Generic.List<>  
            for (var i = 0; i < employee.Count; i++)   // CS0019  Operator '<' cannot be applied to operands of type 'int' and 'method group'   
  
            {  
canvas.DrawString(employee[i].FirstName.ToString(), 50, 50 + 20 * i, HorizontalAlignment.Center);    // CS0021  Cannot apply indexing with[] to an expression of type 'Task<List<Table>>'     
  
       }  
     }  
   }  
}  
  

How to solve the error?
Thanks in advance.

Developer technologies .NET .NET MAUI
Developer technologies .NET .NET Runtime
Developer technologies C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-07-20T13:58:48.217+00:00

    I looked at you code. The Draw method of IDrawable does not support being async (not very modern design), so you can not use async operations inside the method. Extract the async operations to be external to the method, and trigger a redraw when complete.


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-07-12T14:42:27.713+00:00

    Most likely the code that calls Draw() is not using await, and the canvas variable is disposed before Draw() can use it.

    that is you must convert the calling module to async:

    var gd = new GraphicsDrawable();
    ....
    await gd.Draw(canvas, dirtyRect);

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.