示例#1
0
        public void OpenScreen <TScreen>(IScreenFactory <TScreen> factory)
            where TScreen : class, IScreenBase
        {
            ScreenCreationBehavior creationBehavior  = GetCreationBehavior(factory);
            IScreenBase            alreadyOpenScreen = null;

            switch (creationBehavior)
            {
            case ScreenCreationBehavior.MultipleInstances:
                break;

            case ScreenCreationBehavior.SingleInstance:
                alreadyOpenScreen = _screens
                                    .SingleOrDefault(x => x.GetType() == factory.ScreenType);
                break;

            case ScreenCreationBehavior.UseScreenLocation:
                alreadyOpenScreen = Screens
                                    .FirstOrDefault(x => factory.CreatesScreensEquivalentTo(x));
                break;
            }

            bool wasAlreadyOpen = alreadyOpenScreen != null;

            IScreenBase s;

            if (wasAlreadyOpen)
            {
                s = alreadyOpenScreen;
            }
            else
            {
                // If the constructor or Initialize handler of the screen throws an
                // exception we exit here and 'ActiveScreen' is not changed. The screen
                // itself makes sure that is consistently closed in case of an exception.
                s = _screens.AddScreen(factory);
                s.Children
                .Add(new ScreenCloseHandler(requestClose => CloseScreen(s, requestClose)));
            }

            // Activate does handle exceptions correctly and rethrows them, so we skip
            // the publishing of the 'ScreenOpenedEvent'.
            ActiveScreen = s;

            _eventAggregator.Publish(
                ScreenOpenedEvent,
                new ScreenOpenedEventArgs(this, s, wasAlreadyOpen)
                );
        }