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.
Loop Through List
Yusuf
691
Reputation points
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.
Accepted answer
1 additional answer
Sort by: Most helpful
-
Bruce (SqlWork.com) 56,931 Reputation points
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);