我尝试使用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);
                }
            });

        }

    }

最佳答案

第一件事总是在UI线程上调用StackLayout.Children.Clear|Add。 iOS不喜欢从主UI线程中删除UIView子视图时会抛出异常,甚至可能导致本机崩溃

这是我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();
});


注意:try/finally应该包装SemaphoreSlim.Releasecatch以执行添加/清除失败所需的所有恢复程序代码。

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");


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

07-26 02:26