Improve shared MAUI controls and tests

This commit is contained in:
Andy Howard
2026-10-06 14:13:28 +01:00
parent 0406adfc99
commit 5daf7ce97f
106 changed files with 2299 additions and 805 deletions
+25 -36
View File
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Threading.Tasks;
namespace Sarand.Net.Mobile.Maui.Utils
@@ -19,12 +16,10 @@ namespace Sarand.Net.Mobile.Maui.Utils
return MainThread.IsMainThread;
}
//-------------------------------------------------------------------
// I n v o k e o n M a i n T h r e a d a n d W a i t
//-------------------------------------------------------------------
// --- Fire-and-forget dispatch ---
/// <summary>Invoke task on main thread.</summary>
public static void InvokeOnMainThread(Action taskFn)
/// <summary>Invoke an action on the main thread without waiting for completion.</summary>
public static void PostToMainThread(Action taskFn)
{
MainThread.BeginInvokeOnMainThread(() =>
{
@@ -32,38 +27,32 @@ namespace Sarand.Net.Mobile.Maui.Utils
});
}
/// <summary>Invoke task on main thread and wait for completion.</summary>
public static void InvokeOnMainThreadSync(Action taskFn)
// --- Awaitable dispatch ---
/// <summary>Invoke an action on the main thread and await its completion.</summary>
public static Task InvokeOnMainThreadAsync(Action taskFn)
{
var tcs = new TaskCompletionSource<Object>();
MainThread.BeginInvokeOnMainThread(() =>
{
try
{
taskFn();
tcs.SetResult(true);
}
catch (Exception ex) { tcs.SetException(ex); }
});
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
/// <summary>Invoke task on main thread and wait for completion.</summary>
public static T InvokeOnMainThreadSync<T>(Func<T> taskFn)
/// <summary>Invoke an asynchronous function on the main thread and await its completion.</summary>
public static Task InvokeOnMainThreadAsync(Func<Task> taskFn)
{
var tcs = new TaskCompletionSource<T>();
MainThread.BeginInvokeOnMainThread(() =>
{
try
{
var result = taskFn();
tcs.SetResult(result);
}
catch (Exception ex) { tcs.SetException(ex); }
});
return tcs.Task.Result;
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
//-------------------------------------------------------------------
/// <summary>Invoke a function on the main thread and await its result.</summary>
public static Task<T> InvokeOnMainThreadAsync<T>(Func<T> taskFn)
{
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
/// <summary>Invoke an asynchronous function on the main thread and await its result.</summary>
public static Task<T> InvokeOnMainThreadAsync<T>(Func<Task<T>> taskFn)
{
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
// --- The End ---
}
//=======================================================================
}