使用Unity 2.0对“Blendable”ViewModelLocator的建议

时间:2011-08-31 22:57:08

标签: mvvm dependency-injection inversion-of-control viewmodel viewmodellocator

我有一套现有的Silverlight应用程序,它们使用MVVM模式来分离Views和ViewModel。我们将Unity 2.0用于IoC容器,以将依赖项注入ViewModel类(以及支持类型)。我有一个现有的ViewModelLocator类,它使用Unity容器来解析ViewModel。

所有这些在运行时都很有效;但是,因为ViewModelLocator依赖于从App.xaml.cs中的Application_Start方法“运行”的Bootstrapper类创建和配置的Unity容器,所以我无法在设计器或Blend中打开视图

我正在寻找建议如何重写ViewModelLocator以支持“Blendability”。

请注意,为了Blendability,我不愿意强制我们的ViewModel类实现默认的无参数构造函数。我们还让ViewModels检查IsInDesignMode属性(来自MVVM Light ViewModelBase类)以提供设计时数据而不是进行服务调用,因此我们没有针对设计时和运行时的不同ViewModel实现。

让我知道你的想法。

1 个答案:

答案 0 :(得分:2)

您希望UI元素具有良好的设计时体验(包括在Blend中)。这听起来像是一个合理的目标。

让我们看一下UI元素是什么。对于这个答案的其余部分,我将其称为控制。 Control的职责是呈现UI并响应用户事件。只要它应该具有行为,它应该只具有与UI呈现和用户事件相关的行为。

除此之外,框架本身(Silverlight以及WPF)强加了一个规则:所有控件必须具有默认构造函数。此外,由于DataContext是一个属性,因此可以选择它。

我们应该牢记封装。我们开发的任何控件应该在上面给出的约束条件下很好地工作。这意味着,除了拥有默认构造函数之外,还应该在没有DataContext 的情况下正常工作。换句话说,Blendability体验应该由Control本身提供,而不是任何需要引导以分配DataContext的外部容器。

当控件必须响应用户事件时,我总是发现ICommand接口绰绰有余。将ICommands附加到Control中的任何适用的事件处理程序。 ICommands由视图模型定义,但ICommand的优点在于它基本上是一个void方法,这意味着在{的情况下提供(无操作)本地默认值是微不足道的。 {1}}为空。但是,这很少是必要的,因为设计师不会调用命令。


以下是my book的示例:

DataContext

和代码隐藏:

<Window x:Class="Ploeh.Samples.ProductManagement.WpfClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Product Management"
        Height="300"
        Width="300"
        MinHeight="300"
        MinWidth="300">
    <Window.Resources>
        <Style x:Key="ProductStyle" TargetType="{x:Type ListViewItem}">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
        </Style>
    </Window.Resources>
    <DockPanel FocusManager.FocusedElement="{Binding ElementName=productsListView}">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <Separator />
                <MenuItem Header="E_xit" Command="{Binding Path=CloseCommand}" />
            </MenuItem>
            <MenuItem Header="_Actions">
                <MenuItem Header="_Refresh" InputGestureText="F5" Command="{Binding Path=RefreshCommand}" />
                <MenuItem Header="_Add Product" InputGestureText="Ins" Command="{Binding Path=InsertProductCommand}" />
                <MenuItem Header="_Edit Product" InputGestureText="Enter" Command="{Binding Path=EditProductCommand}" />
                <MenuItem Header="_Delete Product" InputGestureText="Del" Command="{Binding Path=DeleteProductCommand}" />
            </MenuItem>
        </Menu>
        <ToolBarTray DockPanel.Dock="Top" HorizontalAlignment="Stretch">
            <ToolBar HorizontalAlignment="Stretch" HorizontalContentAlignment="Left">
                <Button Command="{Binding Path=RefreshCommand}">Refresh</Button>
                <Button Command="{Binding Path=InsertProductCommand}">Add</Button>
                <Button Command="{Binding Path=EditProductCommand}">Edit</Button>
                <Button Command="{Binding Path=DeleteProductCommand}">Delete</Button>
            </ToolBar>
        </ToolBarTray>
        <ListView x:Name="productsListView" ItemContainerStyle="{StaticResource ProductStyle}" ItemsSource="{Binding Path=Products}" SelectionMode="Single">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Path=Id}" />
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Path=UnitPrice}" />
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</Window>
相关问题