MVVM-PRISM,如何在区域/ItemsControl中显示视图的多个实例

发布于 2024-09-28 17:00:40 字数 2332 浏览 4 评论 0原文

我正在为基于 MVVM、CAL 和 PRISM 的应用程序构建一个模块。我对这些概念相当陌生,并试图了解所有模式,现在我正在努力解决以下问题:

我需要创建同一视图的多个实例。每个视图都需要绑定到它自己的 ViewModel,其中包含该特定视图的数据。更详细一点,这是我的场景:

我需要在 OrdersView.xaml 中放置的区域内显示 OrdersDetailsView.xaml 的多个实例。我有一个 OrdersView XAML,其中包含如下定义的 ItemsControl 标记:

<ItemsControl x:Name="OrdersItemsControl" BorderThickness="0" 
cal:RegionManager.RegionName="OrdersRegion" Margin="0,10,0,10">

正如您所理解的 - 在我的 OrdersView 的 ItemsControl 中,我想显示 OrderDetailsView 的多个实例。我可以按照下面描述的硬编码方式添加一个 OrderDetailsView,但是实例化多个视图和数据并将其加载到该区域的正确/首选方法是什么?

Dim OrdersRegion = _RegionManager.Regions("OrdersRegion")
Dim view = _Container.Resolve(Of OrdersDetailsView)()
Dim viewmodel = _Container.Resolve(Of OrdersDetailsViewModel)()

view.ApplyModel(viewmodel)
OrdersRegion.Add(view, "OrdersDetailsView")
OrdersRegion.Activate(view) 

编辑:

为什么这段代码给了我 2 个 OrdersDetailsView ,两者都带有订单 ZZZ,我希望我能得到一个带有订单 XXX 的视图和一个带有订单 ZZZ 的视图​​:

    Dim OrdersRegion = _RegionManager.Regions("OrdersRegion")

    Dim viewX = _Container.Resolve(Of OrdersDetailsView)()
    Dim viewmodelX = _Container.Resolve(Of OrdersDetailsViewModel)()

    viewmodelX.OrdersName = "Orders XXX"
    viewX.ApplyModel(viewmodelX)


    Dim viewZ = _Container.Resolve(Of OrdersDetailsView)()
    Dim viewmodelZ = _Container.Resolve(Of OrdersDetailsViewModel)()

    viewmodelZ.OrdersName = "Orders ZZZ"
    viewZ.ApplyModel(viewmodelZ)

    OrdersRegion.Add(viewX, "OrdersDetailsViewX")
    OrdersRegion.Add(viewZ, "OrdersDetailsViewZ")

编辑2:

Public Sub Initialize() Implements Microsoft.Practices.Composite.Modularity.IModule.Initialize
    RegisterServices()

    container.Resolve(Of IOrdersView)()
    container.Resolve(Of IOrdersViewModel)()
    container.Resolve(Of IOrdersDetailsView)()
    container.Resolve(Of IOrdersDetailsViewModel)()

End Sub

Private Sub RegisterServices()
    container.RegisterType(Of IOrdersViewModel, OrdersViewModel)(New ContainerControlledLifetimeManager())
    container.RegisterType(Of IOrdersView, OrdersView)()

    container.RegisterType(Of IOrdersDetailsViewModel, OrdersDetailsViewModel)(New ContainerControlledLifetimeManager())
    container.RegisterType(Of IOrdersDetailsView, OrdersDetailsView)()
End Sub

I am building an module for an application that is based on MVVM, CAL and PRISM. I'm fairly new to these concepts, and trying to get my head around all the patterns and right now I'm struggling with the following problem:

I am in the need of creating multiple instances of the same View. Each one of the views need to bind to it's own ViewModel containing data for that specific view. A little more detailed, this is my scenario:

I need to display multiple instances of a OrdersDetailsView.xaml within a region placed in OrdersView.xaml. I have an OrdersView XAML that contains an ItemsControl tag defined like this:

<ItemsControl x:Name="OrdersItemsControl" BorderThickness="0" 
cal:RegionManager.RegionName="OrdersRegion" Margin="0,10,0,10">

As you can understand - within the ItemsControl in my OrdersView I want to display multiple instances of a OrderDetailsView. I'm able to add one OrderDetailsView in the hardcoded way described below, but what is the right/preferred way to instanciate and load multiple views and data into the region?

Dim OrdersRegion = _RegionManager.Regions("OrdersRegion")
Dim view = _Container.Resolve(Of OrdersDetailsView)()
Dim viewmodel = _Container.Resolve(Of OrdersDetailsViewModel)()

view.ApplyModel(viewmodel)
OrdersRegion.Add(view, "OrdersDetailsView")
OrdersRegion.Activate(view) 

EDIT:

Why is this code giving me 2 OrdersDetailsView BOTH with Orders ZZZ, I would have hoped that I got one view with Orders XXX and one with Orders ZZZ:

    Dim OrdersRegion = _RegionManager.Regions("OrdersRegion")

    Dim viewX = _Container.Resolve(Of OrdersDetailsView)()
    Dim viewmodelX = _Container.Resolve(Of OrdersDetailsViewModel)()

    viewmodelX.OrdersName = "Orders XXX"
    viewX.ApplyModel(viewmodelX)


    Dim viewZ = _Container.Resolve(Of OrdersDetailsView)()
    Dim viewmodelZ = _Container.Resolve(Of OrdersDetailsViewModel)()

    viewmodelZ.OrdersName = "Orders ZZZ"
    viewZ.ApplyModel(viewmodelZ)

    OrdersRegion.Add(viewX, "OrdersDetailsViewX")
    OrdersRegion.Add(viewZ, "OrdersDetailsViewZ")

EDIT2:

Public Sub Initialize() Implements Microsoft.Practices.Composite.Modularity.IModule.Initialize
    RegisterServices()

    container.Resolve(Of IOrdersView)()
    container.Resolve(Of IOrdersViewModel)()
    container.Resolve(Of IOrdersDetailsView)()
    container.Resolve(Of IOrdersDetailsViewModel)()

End Sub

Private Sub RegisterServices()
    container.RegisterType(Of IOrdersViewModel, OrdersViewModel)(New ContainerControlledLifetimeManager())
    container.RegisterType(Of IOrdersView, OrdersView)()

    container.RegisterType(Of IOrdersDetailsViewModel, OrdersDetailsViewModel)(New ContainerControlledLifetimeManager())
    container.RegisterType(Of IOrdersDetailsView, OrdersDetailsView)()
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2024-10-05 17:00:40

是的,这是正确的做法。当然,您必须重复此代码来创建新的 V-VM 对:

Dim view = _Container.Resolve(Of OrdersDetailsView)()
Dim viewmodel = _Container.Resolve(Of OrdersDetailsViewModel)()

view.ApplyModel(viewmodel)

此外,无需调用 Activate,因为当主机控件是 ItemsControl (AllActiveRegion) 时,所有视图都处于活动状态。

我希望这有帮助。

Yes this is the right way of doing it. Of course, you will have to repeat this code to created new V-VM pairs:

Dim view = _Container.Resolve(Of OrdersDetailsView)()
Dim viewmodel = _Container.Resolve(Of OrdersDetailsViewModel)()

view.ApplyModel(viewmodel)

Also, there is no need to call Activate, as all views are active when the host control is an ItemsControl (AllActiveRegion).

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文