Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 等待Xamarin格式的MessagingCenter_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 等待Xamarin格式的MessagingCenter

C# 等待Xamarin格式的MessagingCenter,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我尝试从ViewModel中使用MessagingCenter实现MVVM。 我获得了以下错误,因为多个线程接收相同的消息“ClearStackLayout”,并且不等待回调的结束: 索引超出了数组的边界 以下是我的查看代码: public partial class LibraryChoicePage : DefaultBackgroundPage { private Object thisLock = new Object(); public Librar

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

        }

    }
public分部类库选择页面:DefaultBackgroundPage{
私有对象thisLock=新对象();
公共图书馆选择页(){
初始化组件();
/*无障碍布局*/
MessagingCenter.Subscribe(此“ClearStackLayout”,(发件人)=>{
锁(这个锁){
此._choices.Children.Clear();
}
});
/*AddToStackLayout*/
MessagingCenter.Subscribe(此“AddToStackLayout”,(发件人,arg)=>{
锁(这个锁){
此._choices.Children.Add(arg);
}
});
}
}

第一件事是始终调用
StackLayout.Children。清除UI线程上的|添加
。iOS不喜欢从主UI线程中删除
UIView
子视图,并且会引发异常,甚至可能导致本机崩溃

这就是我将消息调用
序列化的方式:

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

注意:AddLayout/ClearLayout是
消息中心的方法包装。发送
AddToStackLayout
ClearStackLayout

ClearStackLayout

第一件事是始终调用
StackLayout.Children。在UI线程上清除| Add
。iOS不喜欢从主UI线程中删除
UIView
子视图,并且会引发异常,甚至可能导致本机崩溃

这就是我将消息调用
序列化的方式:

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();
});
注意:AddLayout/ClearLayout是
消息中心的方法包装。发送
AddToStackLayout
ClearStackLayout