【发布时间】:2021-10-05 01:44:14
【问题描述】:
我正在努力使用 WPF 和 MVVM 模式构建应用程序。 在这个应用程序中,我在 MainView 中有三个按钮 - 摄像头、锅炉、温度传感器。当我按下某个按钮时,它会在窗口的第二部分打开附加控件,其中包含有关所选小工具的信息。 我已经做到了,但架构并不好,因为主窗体“知道”某些对象。我的 MainView (XAML) 中有这段代码,它不应该是这样的。
<Window ...>
<Grid>
<Grid.ColumnDefinitions>
</Grid.ColumnDefinitions>
<StackPanel>
<Button Content="Камера"
Command="{Binding NavigationCameraCommand}"
Margin="5"
Height="30"/>
<Button Content="Котёл"
Command="{Binding NavigateBoilerCommand}"
Margin="5"
Height="30"/>
<Button Content="Датчик температуры"
Command="{Binding NavigationTemperatureSensorCommand}"
Margin="5"
Height="30"/>
</StackPanel>
<ContentControl Grid.Column="1" Content="{Binding CurrentViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewmodels:CameraViewModel}">
<views:CameraView/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodels:BoilerViewModel}">
<views:BoilerVIew/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodels:TemperatureSensorViewModel}">
<views:TemperatureSensorView/>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
</Window>
我需要使用 Ninject 来处理这个问题。据我了解,我需要在定位器中定义视图和模型对,然后在注入视图时,将自动确定链接模型,创建它的实例并将其分配给视图上下文。
我还没有找到任何示例。
谁能举例说明如何做到这一点或用一个例子来解释(最好有几个视图)?
我在 YouTube 上找到了这个视频,但仍然不知道它如何在多个观看次数中起作用。 https://www.youtube.com/watch?v=yN4SgWHwhgk
【问题讨论】: