Esempio n. 1
0
        private async Task AsyncOnViewModelWindowsListChanged(AsyncListChangedEventHandler <ChatWindowViewModel> ea)
        {
            switch (ea.ChangedEventArgs.ListChangedType)
            {
            case ListChangedType.ItemAdded:
            {
                await UiWindow.Window.UIThreadAsync(() =>
                    {
                        ChatWindowViewModel newElem = ea.ChangedElemnt;

                        var reslut = _TataruUIModel.ChatWindows.FirstOrDefault(x => x.WinId == newElem.WinId);
                        if (reslut == null)
                        {
                            var sett = newElem.GetSettings();
                            _TataruUIModel.ChatWindows.Add(sett);

                            var binder = new PropertyBinder(_TataruUIModel.ChatWindows[_TataruUIModel.ChatWindows.Count - 1], newElem);
                            CreateBinderCouples(binder);
                            _PropertyBinders.Add(binder);
                        }
                    });
            }
            break;

            case ListChangedType.ItemDeleted:
            {
                await Task.Run(() =>
                    {
                        SpinWait.SpinUntil(() => !deletingObject);
                        lock (lockObj)
                        {
                            deletingObject = true;
                        }
                    });

                await UiWindow.Window.UIThreadAsync(() =>
                    {
                        try
                        {
                            ChatWindowViewModel deletedElem = ea.ChangedElemnt;

                            ChatWindowViewModelSettings reslut = _TataruUIModel.ChatWindows.FirstOrDefault(x => x.WinId == deletedElem.WinId);

                            if (reslut != null)
                            {
                                _TataruUIModel.ChatWindows.Remove(reslut);

                                RemoveChatWindow(_ChatWindows, reslut.WinId);

                                try
                                {
                                    PropertyBinder binder = _PropertyBinders.FirstOrDefault(x => x.Object1 == reslut && x.Object2 == deletedElem);

                                    if (binder == null)
                                    {
                                        binder = _PropertyBinders.FirstOrDefault(x => x.Object2 == reslut && x.Object1 == deletedElem);
                                    }

                                    if (binder != null)
                                    {
                                        binder.Stop();
                                        _PropertyBinders.Remove(binder);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.WriteLog(e);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.WriteLog(e);
                        }
                    });

                deletingObject = false;
            }

            break;
            }
        }