/// <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);
        }
        /// <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);
        }
Beispiel #3
0
 /// <inheritdoc />
 public ViewCompositionContext([NotNull] FrameworkElement control, [NotNull] object dataContext, [NotNull] ICoordinationArguments coordinationArguments)
 {
     Control               = control ?? throw new ArgumentNullException(nameof(control));
     DataContext           = dataContext ?? throw new ArgumentNullException(nameof(dataContext));
     CoordinationArguments = coordinationArguments ?? throw new ArgumentNullException(nameof(coordinationArguments));
 }
 public static void SetCoordinationArguments(DependencyObject element, ICoordinationArguments value)
 {
     element.SetValue(CoordinationArgumentsProperty, value);
 }