Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #40 from dansiegel/scope-fixes
Browse files Browse the repository at this point in the history
Use Container From View
  • Loading branch information
dansiegel authored May 28, 2022
2 parents 73d7572 + d0fa90d commit 3292bb5
Showing 1 changed file with 27 additions and 40 deletions.
67 changes: 27 additions & 40 deletions src/Prism.Maui/PrismAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Prism.Behaviors;
using Prism.Controls;
using Prism.Events;
using Prism.Extensions;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Mvvm;
Expand Down Expand Up @@ -30,7 +29,7 @@ public abstract class PrismAppBuilder

protected PrismAppBuilder(IContainerExtension containerExtension, MauiAppBuilder builder)
{
if(containerExtension is null)
if (containerExtension is null)
throw new ArgumentNullException(nameof(containerExtension));

_container = containerExtension;
Expand Down Expand Up @@ -61,25 +60,26 @@ private void ConfigureViewModelLocator(IContainerProvider container)
return bindable.GetValue(ViewModelLocator.ViewModelProperty) as Type;
});

ViewModelLocationProvider2.SetDefaultViewModelFactory((view, type) =>
ViewModelLocationProvider2.SetDefaultViewModelFactory(DefaultViewModelLocator);
}

internal static object DefaultViewModelLocator(object view, Type viewModelType)
{
if (view is not BindableObject bindable)
return null;

var container = bindable.GetValue(Navigation.Xaml.Navigation.NavigationScopeProperty) as IContainerProvider;

var overrides = new List<(Type Type, object Instance)>();
if (container.IsRegistered<IResolverOverridesHelper>())
{
var overrides = new List<(Type Type, object Instance)>();
if (container.IsRegistered<IResolverOverridesHelper>())
{
var resolver = container.Resolve<IResolverOverridesHelper>();
var resolverOverrides = resolver.GetOverrides();
if (resolverOverrides.Any())
overrides.AddRange(resolverOverrides);
}

if (!overrides.Any(x => x.Type == typeof(INavigationService)))
{
var navService = CreateNavigationService(container, view);
overrides.Add((typeof(INavigationService), navService));
}

return container.Resolve(type, overrides.ToArray());
});
var resolver = container.Resolve<IResolverOverridesHelper>();
var resolverOverrides = resolver.GetOverrides();
if (resolverOverrides.Any())
overrides.AddRange(resolverOverrides);
}

return container.Resolve(viewModelType, overrides.ToArray());
}

public PrismAppBuilder RegisterTypes(Action<IContainerRegistry> registerTypes)
Expand Down Expand Up @@ -133,14 +133,15 @@ public MauiAppBuilder OnAppStart(Action<IContainerProvider, INavigationService>
public PrismAppBuilder ConfigureDefaultViewModelFactory(Func<IContainerProvider, object, Type, object> viewModelFactory)
{
ViewModelLocationProvider2.SetDefaultViewModelFactory((view, type) =>
viewModelFactory(_container, view, type));
{
if (view is not BindableObject bindable)
return null;

return this;
}
var container = bindable.GetValue(Navigation.Xaml.Navigation.NavigationScopeProperty) as IContainerProvider;
return viewModelFactory(container, view, type);
});

public MauiApp Build()
{
return MauiBuilder.Build();
return this;
}

private void RegistrationCallback(IContainerExtension container)
Expand All @@ -165,18 +166,4 @@ private void RegisterDefaultRequiredTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterPageBehavior<PageLifeCycleAwareBehavior>();
containerRegistry.RegisterPageBehavior<PageScopeBehavior>();
}

private INavigationService CreateNavigationService(IContainerProvider container, object view)
{
if (view is Page page)
{
return Navigation.Xaml.Navigation.GetNavigationService(page);
}
else if (view is VisualElement visualElement && visualElement.TryGetParentPage(out var parent))
{
return Navigation.Xaml.Navigation.GetNavigationService(parent);
}

return container.Resolve<INavigationService>();
}
}

0 comments on commit 3292bb5

Please # to comment.