我正在使用 Prism 7 和 .Net 6 和 MediatR 開發應用程式,我的 app.xaml 是
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
}
class Bootstrapper : PrismBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//I Want To Use Unity Here
}
}
問題是我想使用為 IUnityContainer 設計的擴展方法,但我不能,因為我在 Bootstrapper 的 RegisterTypes 方法中有 IContainerRegistry 而不是 IUnityContainer
public static IUnityContainer RegisterMediator(this IUnityContainer container, ITypeLifetimeManager lifetimeManager)
{
return container.RegisterType<IMediator, Mediator>(lifetimeManager)
.RegisterInstance<ServiceFactory>(type =>
{
var enumerableType = type
.GetInterfaces()
.Concat(new[] { type })
.FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
return enumerableType != null
? container.ResolveAll(enumerableType.GetGenericArguments()[0])
: container.IsRegistered(type)
? container.Resolve(type)
: null;
});
}
prism 支持幾個 IoC Container,DryIoc,shiny,但是真的沒有檔案如何更改默認的 IoC Container,如何選擇 WPF Prism Application 使用哪個 IoC Container?在這種情況下,我想選擇 Unity,這樣我就可以使用我的 mediatR 擴展,我已經搜索了整個網站但沒有成功。謝謝你
uj5u.com熱心網友回復:
在Prism.Unity命名空間中,有一個擴展方法IContainerProvider.
public static IUnityContainer GetContainer(this IContainerProvider containerProvider)
你可以在你的PrismBootstrapper.
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
IUnityContainer container = Container.GetContainer();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489999.html
下一篇:如何從<ItemsControl.ItemTemplate><DataTemplate>更改代碼中的邊框寬度和高度
