带有MVVM设计的数据绑定Windows Phone全景图 [英] Databound windows phone panorama with MVVM design
问题描述
请协助解决MVVM设计/理解问题.
Please assist with MVVM design/understanding problem.
鉴于我们有一个具有以下UI结构的Windows Phone应用:
Given that we have a Windows Phone app with following UI structure:
MainPage.xaml 正文:
<views:PanoramaView/>
DataContext is set via MVVM Light view-model locator to a static MainViewModel class instance.
Views/PanoramaView.xaml 正文:
<UserControl.DataContext>
<ViewModels:PanoramaViewModel/>
</UserControl.DataContext>
<StackPanel x:Name="LayoutRoot">
<controls:Panorama Background="{Binding PanoramaBackgroundBrush}"
ItemsSource="{Binding PanoramaItems}"
ItemTemplate="{StaticResource panoramaItemTemplate}"
/>
</StackPanel>
那时我偶然发现了一个问题-如果我希望我所有的PanoramaItems都由不同的用户控件组成,该怎么办?如果我定义了全景图项目模板,那我就注定了他们都一样.但是我的目的是要拥有多个完全不同的全景项目.我想拥有一个类(大概是PanoramaViewModel
),该类可以让我控制在给定的时间显示哪些全景图项目.
At that point I have stumbled upon a question - What should I do in case I want all my PanoramaItems to be comprised of a different user controls? If I define a panorama item template, I doom all of them to be alike. But my intention is to have serveral, compeltely different panorama items. I wanted to have a class (presumably PanoramaViewModel
) that would allow me control which panorama items are displayed at a given moment of time.
因此,必须有一种方法让我仍然坚持使用MVVM,但能够实例化新的Views(全景项目)并将其注入到我的PanoramaViewModel
的PanoramaItems
集合中.我在哪里以及如何做?
So there has to be a way for me to still stick to MVVM, but to be able to instantiate new Views(Panorama Items) and inject them into a PanoramaItems
collection of my PanoramaViewModel
. Where and how do I do that?
推荐答案
您必须定义资源键,以使用用于不同视图模型类类型的视图项的视图设置器定义数据模板,并从中派生VM类.一个通用的基类(即PanoramaViewModel)
You have to define resource key to define a data template with view setter for the view item being rendered for the different view model class types, and derive VM classes from a common base class (PanoramaViewModel, i.e)
这篇关于带有MVVM设计的数据绑定Windows Phone全景图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!