c# - 如何在核心项目和具有更改页面的 WPF 应用程序之间使用依赖注入?
问题描述
我正在使用框架和for构建一个WPF
程序。我创建了两个项目,一个用于其他应用程序的核心项目和一个特定应用程序。MVVM
Ninject
Dependancy Injection
.Net Class Library
.Net
WPF
目前,我正在使用ApplicationViewModel
带有Property
CurrentPage
. CurrentPage
是一种Enum
称为ApplicationPage
包含我的应用程序中不同页面的类型。在我的 WPF 应用程序的 MainWindow 中有一个框架,它Content
使用值bound
转换CurrentPage Property
器将值转换为CustomPages
我使用switch
语句所做的不同值,如下所示:
if (value is ApplicationPage)
switch ((ApplicationPage)value)
{
case ApplicationPage.PageOne:
return new PageOne();
case ApplicationPage.PageTwo:
return new PageTwo();
default:
throw Exception;
}
}
我想使用将这些页面Constructor
Injection
传递到一个内部,使用它又进入到类中,有点像这样:View Models
Page's
Constructor
Converter
ViewModels
Injected
ApplicationViewModel
case ApplicationPage.PageOne:
return new PageOne(PageOneViewModel);
我的第一个想法是,是否有某种方法可以使CurrentPage
Property
实际 a 具体ViewModel
并执行 aswitch
以便ViewModel
将Converter
a 转换ViewModel
为 a Page
?
但是Type ofCurrentPage
是一个问题,因为它必须设置为 之一,ViewModels
因此不能取 a different 的值ViewModel
,让您只能使用一个ViewModel
Class
。
我的想法是:有没有办法传递ViewModel
到Converter
?或者我可以从工厂设置CurrentPage
并在转换器内IViewModelFactory
创建吗?ViewModel
在这种情况下,我将如何更改 的值CurrentPage
以更改应用程序中的页面?
在遵循这个逻辑的同时有没有办法坚持Dependency
Injection
,还是有另一种方法,我是否需要重新考虑我的页面更改代码?不幸的是,我见过的大多数例子都属于所谓的ServiceLocator
反模式。
解决方案
答案是使用数据模板如下,
<Window.Resources>
<DataTemplate x:Key="View1Template" DataType="{x:Type local:MainWindowViewModel}">
<!-- Custom control style with a Data Context set to a Viewmodel
object in the MainWindowViewModel -->
<local:CustomPage1 DataContext="{Binding CustomPage1ViewModel}" />
</DataTemplate>
<DataTemplate x:Key="View2Template" DataType="{x:Type local:MainWindowViewModel}">
<!-- Custom control style with a Data Context set to a Viewmodel
object in the MainWindowViewModel -->
<local:CustomPage2 DataContext="{Binding CustomPage2ViewModel}" />
</DataTemplate>
</Window.Resources>
然后Data Trigger
在Content Control Style
;
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<!-- The Default/initial View being shown -->
<Setter Property="ContentTemplate" Value="{StaticResource View1Template}" />
<!-- Triggers bound to the CurrentView Property -->
<Style.Triggers>
<DataTrigger Binding="{Binding CurrentView}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource View1Template}" />
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}" Value="2">
<Setter Property="ContentTemplate" Value="{StaticResource View2Template}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
是CurrentView
一个属性,可以在代码中Trigger
更改为UI
- 可以设置为Enum
的更改PageNames
。
推荐阅读
- amazon-web-services - Aws TTS Polly -> 如何检查当前使用情况(已使用的字符数)?
- .net-core - 最新版 System.Data.Sqlite 不支持密码加密吗?
- c# - 无法识别时如何在.Net中序列化枚举
- r - 如何在条件中引用向量的元素?
- reactjs - 表情符号在 contentEditable 中重新渲染后变成问号
- r - 使用 fviz_clusters() 可视化 K-means 集群时遇到问题
- r - 有没有一种方便的方法可以在 Raku 中复制 R 的“命名向量”概念,可能使用 Mixins?
- r - 在循环中将 gtables 与单独的标题结合起来
- google-sheets - 谷歌工作表根据文本将背景更改为多列
- linux - 使用内存文件系统在 golang 中测试系统软件