private void FinalizeControls()
        {
            Tetris?.Dispose();

            Tetris = null;

            PrevFigureSet = 0;
            PrevColorSet  = 0;

            this.InvalidateMeasure();
        }
        private void InvokeSetFiguresAndColors()
        {
            bool FiguresChanged = false;
            bool ColorsChanged  = false;

            // фигуры

            if (FigureSet == TetrisFiguresEnum.Random || FigureSet != PrevFigureSet)
            {
                FiguresChanged = true;

                FigureSetChanged |= FigureSet == TetrisFiguresEnum.Random || (FigureSet != PrevFigureSet && PrevFigureSet != 0);

                // определяем набор фигур

                TetrisFiguresEnum ChoosenFigureSet;

                TetrisFigures Figures;

                if (FigureSet == TetrisFiguresEnum.Random)
                {
                    var List = Enum.GetValues(typeof(TetrisFiguresEnum)).Cast <TetrisFiguresEnum>().Where(e => e != PrevFigureSet && e != TetrisFiguresEnum.Random);

                    if (List.Count() == 0)
                    {
                        throw new Exception("No figure sets to choose");
                    }

                    ChoosenFigureSet = List.ElementAt(Random.Next(List.Count()));
                }
                else
                {
                    ChoosenFigureSet = FigureSet;
                }

                PrevFigureSet = ChoosenFigureSet;

                // получаем набор фигур

                switch (ChoosenFigureSet)
                {
                case (TetrisFiguresEnum.Standard):
                    Figures = new TetrisFiguresStandard();
                    break;

                case (TetrisFiguresEnum.Figures2x2):
                    Figures = new TetrisFiguresRandom(2, true, 2, 4);
                    //Figures = new TetrisFiguresRandom(4, true, 16, 16);
                    break;

                case (TetrisFiguresEnum.Figures3x3):
                    Figures = new TetrisFiguresRandom(3, true, 2, 5);
                    break;

                case (TetrisFiguresEnum.Figures4x4):
                    Figures = new TetrisFiguresRandom(4, true, 2, 5);
                    break;

                default:
                    throw new Exception("Unknown figure set");
                }

                // устанавливаем набор фигур

                Tetris.Figures = Figures;

                if (Application.Current.Dispatcher.CheckAccess())
                {
                    InitializeNextFigure();
                }
                else
                {
                    Application.Current.Dispatcher.BeginInvoke((Action)(() => InitializeNextFigure()), DispatcherPriority.Normal);
                }
            }

            // цвета

            if (ColorSet == TetrisFigureColorsEnum.Random || ColorSet != PrevColorSet)
            {
                ColorsChanged = true;

                // определяем набор цветов

                TetrisFigureColorsEnum ChoosenColorSet;

                TetrisFigureColors FigureColors;

                if (ColorSet == TetrisFigureColorsEnum.Random)
                {
                    var List = Enum.GetValues(typeof(TetrisFigureColorsEnum)).Cast <TetrisFigureColorsEnum>().Where(e => e != PrevColorSet && e != TetrisFigureColorsEnum.Random);

                    if (List.Count() == 0)
                    {
                        throw new Exception("No color sets to choose");
                    }

                    int i = Random.Next(List.Count());

                    ChoosenColorSet = List.ElementAt(i);
                }
                else
                {
                    ChoosenColorSet = ColorSet;
                }

                PrevColorSet = ChoosenColorSet;

                // получаем набор цветов

                switch (ChoosenColorSet)
                {
                case (TetrisFigureColorsEnum.Standard):
                    FigureColors = new TetrisColorsStandard();
                    break;

                case (TetrisFigureColorsEnum.Dark):
                    FigureColors = new TetrisColorsDark();
                    break;

                case (TetrisFigureColorsEnum.Gray):
                    FigureColors = new TetrisColorsGray();
                    break;

                case (TetrisFigureColorsEnum.White):
                    FigureColors = new TetrisColorsWhite();
                    break;

                case (TetrisFigureColorsEnum.Black):
                    FigureColors = new TetrisColorsBlack();
                    break;

                default:
                    throw new Exception("Unknown color set");
                }

                // устанавливаем набор цветов

                Tetris.FigureColors = FigureColors;
            }

            // следующая фигура

            if (Tetris.NextFigure != null)
            {
                if (FiguresChanged)
                {
                    Tetris.CreateNextFigure();
                }
                else if (ColorsChanged)
                {
                    Tetris.ColorNextFigure();
                }
            }
        }