简体   繁体   English

如何使用Prism和Unity实现“View Model First”?

[英]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.

相关问题 如何/在哪里使用PRISM和Unity实例化MVVM中的模型对象 - How/Where to instantiate model objects in MVVM using PRISM and Unity 如何使用Prism和Unity显示视图的多个实例 - How display multiple instances of a view using Prism and Unity 棱镜:ViewModelLocator创建视图模型实例,但是我该如何定位呢? - Prism: ViewModelLocator creates the view model instance(s) but how can I target it? 如何让 Prism 的 DelegateCommand 观察子视图模型的属性? - How can I make Prism's DelegateCommand observe a child view-model's property? 使用Prism和Unity在不同的模块中切换视图 - Switch View in different Modules using Prism and Unity 使用Unity IoC时如何实现Ninject .InSingletonScope() - How can I implement Ninject .InSingletonScope() when using Unity IoC Prism Unity背景视图 - Prism Unity background view 在Xamarin上使用Prism时,如何警告View和ViewModel上的名称不匹配? - When using Prism on Xamarin, how can I warn if the names on the View, and ViewModel don't match? 我如何在WPF棱镜中使用不同的ViewModel重用同一视图? - How can I reuse the same view with different ViewModel in wpf prism? 使用 Prism 导航时如何处理视图? - How can I dispose a view when navigating away with Prism?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM