在大型项目中,您如何组织WPF资源?
即使是我所做的最小的WPF示例和原型,的在大型项目中,您如何组织WPF资源?,wpf,xaml,resources,Wpf,Xaml,Resources,即使是我所做的最小的WPF示例和原型,的也开始快速膨胀。将它放回app.xaml会使它完全脱离我的Windows和用户控件,但它很难组织(Visual Studio的“xaml折叠”功能没有任何帮助,因为你的页面上满是“样式…”一词) 此外,我正在努力找到一种易于记忆和组织的方式来命名我的风格。我发现最好的方法就是长而有描述性,所以我得到了这样的东西:BottomMainLeftScrollViewerStyle,等等。但这有它的局限性,很快就会让人困惑。我决定对样式名使用camelCase,以
也开始快速膨胀。将它放回app.xaml
会使它完全脱离我的Windows和用户控件,但它很难组织(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>
真的
使用单独的ResourceDictionary
s,并根据需要将它们合并到可视化树中的适当级别
<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>
只需添加一个简短的注释,如果您还需要向上面添加其他资源,请将它们放在MergedDictionaries部分之后。。。ie:。。。非常简单,但不是很明显。“…将它们合并到视觉树中的适当级别…”:这是什么意思?我了解App.xaml中MergedDictionaries集合中列出的资源文件是什么。mergig进入“适当级别”是否更复杂?