小编Nil*_*One的帖子

在Xamarin Forms中等待MessagingCenter

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

c# xamarin xamarin.forms

1
推荐指数
1
解决办法
3465
查看次数

标签 统计

c# ×1

xamarin ×1

xamarin.forms ×1