[英]How can I implement “View Model First” using Prism and Unity?
Clarification I am working with an MVVM solution. 澄清我正在使用MVVM解决方案。 I have a 1 to 1 mapping between ViewModels and Views.
我在ViewModels和Views之间有一对一的映射。 All solutions I have seen follow a view first approach where the View type is resolved by an IoC container and has a ViewModel as a dependency.
我见过的所有解决方案都遵循第一种方法,其中View类型由IoC容器解析,并具有ViewModel作为依赖关系。 I need to reverse that somehow.
我需要以某种方式扭转这种局面。
Original post: 原帖:
I am currently trying to refactor a simple database viewing application from Caliburn Micro to Prism (which I am very new to) . 我目前正在尝试重构一个简单的数据库查看应用程序,从Caliburn Micro到Prism (我很新) 。 The application currently utilizes a ViewModel-First approach and the ShellViewModel maintains a list of ViewModels that is bound to a TabControl.
该应用程序当前使用ViewModel-First方法,ShellViewModel维护一个绑定到TabControl的ViewModel列表。 I can not find how to implement a similar approach in Prism.
我找不到如何在Prism中实现类似的方法。 All solutions I have seen use a view first approach, but I have multiple states all mapping to one type of view and need to keep those states separate.
我见过的所有解决方案都使用了第一种视图方法,但我有多个状态都映射到一种类型的视图,需要将这些状态分开。
Is there a way I can configure prism to automatically inject a view when a viewmodel is assigned to a region? 我有没有办法配置棱镜在视图模型分配到区域时自动注入视图?
Thank you. 谢谢。
Rachel pointed me to a solution in her comment to the original question. 雷切尔在对原始问题的评论中指出了我的解决方案。 Instead of trying to implement special prism functionality and prism regions, I have gone with a more straight forward MVVM implementation using DataTemplates.
我没有尝试实现特殊的棱镜功能和棱镜区域,而是使用DataTemplates进行了更直接的MVVM实现。
ViewModel outline: ViewModel大纲:
public abstract class ContainerViewModel : BindableBase
{
public ObservableCollection<ItemViewModel> Items { get; set; }
public ItemViewModel ActiveItem { get; set; }
protected virtual void Add(ItemViewModel item) { ... }
protected virtual void Remove(ItemViewModel item) { ... }
protected virtual void Activate(ItemViewModel item) { ... }
}
And XAML: 和XAML:
<TabControl Grid.Column="1" ItemsSource="{Binding Items}" SelectedItem="{Binding ActiveItem}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Table.TableName}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate DataType="{x:Type viewModels:QueryViewModel}">
<local:QueryView />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
看看这个代码项目文章(忽略关于子容器的部分): http : //www.codeproject.com/Articles/640573/ViewModel-st-Child-Container-PRISM-Navigation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.