您如何在大型项目中保持WPF资源的组织?

时间:2009-04-27 15:43:58

标签: wpf xaml resources

即使是我一直在做的最小的WPF示例和原型, <Windows.Resources>开始膨胀。把它放回到app.xaml会把它全部从我的Windows和UserControls中解脱出来,但难以整理(Visual Studio“XAML折叠”功能没有任何帮助,因为你只有一个页面充满了“风格......”这个词。)

此外,我正在努力想出一个易于记忆和有条理的命名我的风格的方式。最好的方法我发现它只是为了冗长和描述性,所以我得到这样的东西:BottomMainLeftScrollViewerStyle等。但这有其局限性,很快也会让人感到困惑。我决定使用 camelCase作为样式名称,以便在XAML的页面和页面中轻松发现它们。

您有什么策略可以防止WPF资源变得笨拙?

<Window.Resources>

    <local:CutOffConverter x:Key="AgeConverter" Cutoff="30"/>

    <Style TargetType="Grid" x:Key="customerGridMainStyle">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint=".5,.5">
                    <GradientStop Offset="0.0" Color="#888"/>
                    <GradientStop Offset="1.0" Color="#ccc"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="StackPanel" x:Key="mainStackPanelStyle">
        <Setter Property="HorizontalAlignment" Value="Left"/>
    </Style>
    <Style TargetType="ScrollViewer" x:Key="mainScrollViewerStyle">
        <Setter Property="Height" Value="250"/>
    </Style>
    <Style TargetType="ListBox" x:Key="mainListBoxStyle">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Margin" Value="10"/>
    </Style>


    <ObjectDataProvider x:Key="customers"
                        ObjectType="{x:Type local:Customer}"
                        MethodName="GetAllCustomers"/>

    <DataTemplate DataType="{x:Type local:Customer}">
        <Grid x:Name="MainGrid" Style="{StaticResource customerGridMainStyle}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="150"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="First Name" Margin="5"/>
            <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" Margin="5"/>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="Last Name" Margin="5"/>
            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding LastName}" Margin="5"/>
            <TextBlock Grid.Column="0" Grid.Row="2" Text="Age" Margin="5"/>
            <TextBlock x:Name="Age" Grid.Column="1" Grid.Row="2" Text="{Binding Age}" Margin="5"/>
        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=Age, Converter={StaticResource AgeConverter}}">
                <DataTrigger.Value>true</DataTrigger.Value>
                <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</Window.Resources>

1 个答案:

答案 0 :(得分:15)

使用单独的ResourceDictionary并根据需要将它们合并到可视树中的相应级别。

<App ...>
    <App.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ListBoxResources.xaml"/>
                <ResourceDictionary Source="ComboBoxResources.xaml"/>
                <ResourceDictionary Source="LabelResources.xaml"/>
                <ResourceDictionary Source="TextBoxResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <!-- if you have local resources, place them here.
                (as noted by Mark Synowiec in the comments)
             -->
        </ResourceDictionary>
    </App.Resources>
</App>