我尝试使用我的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);
}
});
}
}
答案 0 :(得分:5)
首要问题是 总是 在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.Release
和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");
注意:AddLayout / ClearLayout是MessageCenter.Send
和AddToStackLayout
的{{1}}的方法包装。