在Xamarin Forms中等待MessagingCenter

Nil*_*One 1 c# xamarin xamarin.forms

我尝试使用我的ViewModel中的MessagingCenter实现MVVM.我获得了以下错误,因为多个线程收到相同的消息"ClearStackLayout",并且不等待回调的结束:

指数数组的边界之外.

这是我的查看代码:

public partial class LibraryChoicePage : DefaultBackgroundPage {

        private Object thisLock = new Object();

        public LibraryChoicePage() {
            InitializeComponent();

            /* ClearStackLayout */
            MessagingCenter.Subscribe<LibraryChoiceViewModel>(this, "ClearStackLayout", (sender) => {
                lock (thisLock) {
                    this._choices.Children.Clear();
                }
            });

            /* AddToStackLayout */
            MessagingCenter.Subscribe<LibraryChoiceViewModel, View>(this, "AddToStackLayout", (sender, arg) => {
                lock (thisLock) {
                    this._choices.Children.Add(arg);
                }
            });

        }

    }
Run Code Online (Sandbox Code Playgroud)

Sus*_*ver 6

最重要的是始终StackLayout.Children.Clear|Add在UI线程上调用.iOS UIView从主UI线程中删除子视图时不喜欢,并且会抛出异常,甚至可能导致本机崩溃

这就是serialized消息调用的方式:

var semaphone = new SemaphoreSlim(1);
MessagingCenter.Subscribe<object>(this, "ClearStackLayout",  async (sender) =>
{
    await semaphone.WaitAsync();
    Device.BeginInvokeOnMainThread(() =>
    {
        _choices.Children.Clear();
    });
    semaphone.Release();
});

MessagingCenter.Subscribe<object, View>(this, "AddToStackLayout", async (sender, arg) =>
{
    await semaphone.WaitAsync();
    Device.BeginInvokeOnMainThread(() =>
    {
        _choices.Children.Add(arg);
    });
    semaphone.Release();
});
Run Code Online (Sandbox Code Playgroud)

注意:try/finally应该包装SemaphoreSlim.Release和a catch来执行添加/清除失败所需的任何恢复器代码.

UIUnit并行测试方法:

Random random = new Random();
var tasks = new List<Task>();
for (int i = 0; i < 50; i++)
{
    if (random.NextDouble() > .1)
        tasks.Add(Task.Factory.StartNew(() => { AddLayout(); }));
    else
        tasks.Add(Task.Factory.StartNew(() => { ClearLayout(); }));
}
var completed = Task.Factory.ContinueWhenAll(tasks.ToArray(), (messagecenterTasks) => { 
    foreach (var task in messagecenterTasks)
    {
        if (task.Status == TaskStatus.Faulted)
        {
            D.WriteLine("Faulted:");
            D.WriteLine($"  {task.Exception.Message}");
        }
    }
}).Wait(1000);
if (!completed)
    D.WriteLine("Some tasks did not complete in time allocated");
Run Code Online (Sandbox Code Playgroud)

注:AddLayout/ClearLayout是方法包装MessageCenter.SendAddToStackLayoutClearStackLayout.