I am sending an intent I am receiving through a BroadcastReceiver
to multiple Views in my shared-project. The DisplayResult
-method implements the MessagingCenter.Send
.
public class MyBroadcast : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
String action = intent.Action;
if (action.Equals(MainActivity.Instance.Resources.GetString(intentString)))
{
//Device.BeginInvokeOnMainThread(() => MainActivity.Instance.DisplayResult(intent));
//Task.Run(() => MainActivity.Instance.DisplayResult(intent));
//MainActivity.Instance.RunOnUiThread(() => MainActivity.Instance.DisplayResult(intent));
}
}
}
If a message is received I want to update my Views e.g. like this:
MessagingCenter.Subscribe<object, Model>(this, "HI", (sender, arg) =>
{
var dt = DateTime.Now;
_logger.Debug($"Task started: {dt}");
ActivityIndicator.IsVisible = true;
ActivityIndicator.IsRunning = true;
Task.Run(async () =>
{
await SomeTask();
}).GetAwaiter().GetResult();
_logger.Debug($"Task finished: {DateTime.Now - dt}");
ActivityIndicator.IsRunning = false;
ActivityIndicator.IsVisible = false;
}
});
The issue is if I use the approach with Task.Run(...)
my Views are only showing up once but the ActivityIndicator
is running. That means if I close a View and go back to the MainPage and navigate to another or the same View again it only shows a white screen.
If I use either Device.BeginInvokeOnMainThread(...)
or RunOnUiThread(...)
I get the skipped frames error. And my app is frozen until every Task within the MessagingCenter.Subscribe
in my Views is finished. The ActivityIndicator
is Not showing up.