/// <inheritdoc />
        public async Task <bool> DisplayAsync(object dataContext, ICoordinationArguments coordinationArguments)
        {
            if (coordinationArguments is RegionArguments arguments)
            {
                var control = RegionManager.GetControl(arguments.RegionManagerReference, arguments.TargetRegion);

                if (control.DataContext is IBehaviorHost interactive)
                {
                    var context = new ContentChangingBehaviorContext(ServiceContext.ServiceProvider, control.DataContext, dataContext);
                    await BehaviorRunner.ExecuteAsync(interactive, context);

                    if (context.Cancelled)
                    {
                        Log.Debug($"Change prevented by {nameof(ContentChangingBehaviorContext)}.");
                        return(false);
                    }
                }

                var composer = ComposerFactory.Create(control);
                if (composer == null)
                {
                    return(false);
                }

                return(await composer.ComposeAsync(new ViewCompositionContext(control, dataContext, coordinationArguments)));
            }

            return(false);
        }
        public ComposeResult <TranslationUnit> Compose(ICollection <Token> tokens)
        {
            var result = new TranslationUnit();

            var declarationComposer = ComposerFactory.Create <Declaration>();

            result.Declarations = InfiniteSequence.Create <int>()
                                  .Select(v =>
            {
                var r  = declarationComposer.Compose(tokens);
                tokens = r.Tokens;
                return(r.Result);
            })
                                  .TakeWhile(v => v.HasValue)
                                  .Select(v => v.Value)
                                  .ToList();

            var statementComposer = ComposerFactory.Create <Statement>();

            result.Statements = InfiniteSequence.Create <int>()
                                .Select(v =>
            {
                var r  = statementComposer.Compose(tokens);
                tokens = r.Tokens;
                return(r.Result);
            })
                                .TakeWhile(v => v.HasValue)
                                .Select(v => v.Value)
                                .ToList();

            return(new ComposeResult <TranslationUnit> {
                Result = result.ToMaybe(), Tokens = tokens
            });
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public async Task <bool> DisplayAsync(object dataContext, ICoordinationArguments coordinationArguments)
        {
            if (dataContext == null)
            {
                throw new ArgumentNullException(nameof(dataContext));
            }
            if (coordinationArguments == null)
            {
                throw new ArgumentNullException(nameof(coordinationArguments));
            }

            if (coordinationArguments is WindowArguments arguments)
            {
                if (!WindowManager.TryGetWindow(arguments.WindowId, out var window))
                {
                    Log.Debug($"Creating window for {dataContext.GetType().FullName} using id [{arguments.WindowId}]");
                    foreach (var windowFactory in WindowFactories)
                    {
                        if (windowFactory.CanCreateWindow(dataContext))
                        {
                            window = windowFactory.CreateWindow(dataContext);
                            WindowManager.RegisterWindow(window, arguments.WindowId);
                        }
                    }

                    if (window == null)
                    {
                        Log.Error($"No factory was able to create a window for {dataContext.GetType().FullName}");
                        return(false);
                    }
                }

                var composer = ComposerFactory.Create(window);
                if (composer == null)
                {
                    return(false);
                }

                if (window.DataContext is IBehaviorHost interactive)
                {
                    var context = new ContentChangingBehaviorContext(ServiceContext.ServiceProvider, window.DataContext, dataContext);
                    await BehaviorRunner.ExecuteAsync(interactive, context);

                    if (context.Cancelled)
                    {
                        Log.Debug($"Change prevented by {nameof(ContentChangingBehaviorContext)}.");
                        return(false);
                    }
                }

                return(await composer.ComposeAsync(new ViewCompositionContext(window, dataContext, coordinationArguments)));
            }

            Log.Error($"Unable to visualize {dataContext} because {nameof(arguments)} is not of type {typeof(WindowArguments).FullName}");
            return(false);
        }