.net 使用NavigationService的WPF依赖项注入
我正在寻找在我的WPF应用程序中使用DI的更好方法。 我使用frame和NavigationService在选项卡之间进行导航。 我将WPF与.NET framework 4.7.2、EF Core一起使用,对于DI,我将使用Microsoft.Extensions.DependencyInjection(来自.NET Core的DI) 一切正常,但在页面之间导航可能会有点混乱。 我的主窗口如下所示,正在加载依赖项。然而,我想在UserProfile页面中使用IPersonOrchestration,我必须在参数中传递它才能在那里使用它.net 使用NavigationService的WPF依赖项注入,.net,wpf,dependency-injection,.net,Wpf,Dependency Injection,我正在寻找在我的WPF应用程序中使用DI的更好方法。 我使用frame和NavigationService在选项卡之间进行导航。 我将WPF与.NET framework 4.7.2、EF Core一起使用,对于DI,我将使用Microsoft.Extensions.DependencyInjection(来自.NET Core的DI) 一切正常,但在页面之间导航可能会有点混乱。 我的主窗口如下所示,正在加载依赖项。然而,我想在UserProfile页面中使用IPersonOrchestrati
private readonly IPersonOrchestration_personOrchestration;
public MainWindow(IPersonOrchestration personOrchestration)
{
_personOrchestration = personOrchestration;
InitializeComponent();
_mainFrame.NavigationService.Navigate(new UserProfile(personOrchestration));
}
我的UserProfile页面,我想在其中使用该编排:
private readonly IPersonOrchestration_personOrchestration;
public UserProfile(IPersonOrchestration personOrchestration)
{
_personOrchestration = personOrchestration;
InitializeComponent();
}
在UserProfile中,将有更多的步骤,通过这个实现,我必须在每个步骤的每个参数中从MainWindow传递编排。
有没有一种方法可以直接在UserProfile中初始化依赖项,而不通过导航传入参数?好的,我找到了一个可能的解决方案(尽管我不认为这是最好的方法) 将主窗口更改为此:
public readonly IPersonOrchestration _personOrchestration;
public static MainWindow AppWindow
public MainWindow(IPersonOrchestration personOrchestration)
{
AppWindow = this;
_personOrchestration = personOrchestration;
InitializeComponent();
_mainFrame.NavigationService.Navigate(new UserProfile());
}
编排是公开的,我向主窗口添加了一个公开引用,因为每个其他页面都是主窗口的“子”页面。
然后在UserProfile中加载业务流程,如下所示:
private readonly IPersonOrchestration _personOrchestration;
public UserProfile()
{
if (MainWindow.AppWindow?._personOrchestration!= null)
_personOrchestration = MainWindow.AppWindow._personOrchestration;
InitializeComponent();
}