Tôi thường tạo một dự án tạo kiểu riêng biệt, mà tôi tham khảo từ các dự án mà tôi muốn tạo kiểu. Dự án phong cách có một cấu trúc cố định như thế này:
Đối với mỗi kiểm soát, tôi tạo ra một phong cách ResourceDictionary
. Ví dụ cho các nút của tôi:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="PrimaryButtonStyle" TargetType="Button">
</Style>
<Style x:Key="ToolbarButton" TargetType="Button">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="3"/>
<Setter Property="Background" Value="Transparent"></Setter>
</Style>
</ResourceDictionary>
Trong một chính ResourceDictionary
, tôi merge tất cả các từ điển khác, trong trường hợp này trong file IncaDesign.xaml, mà bạn có thể nhìn thấy trong hình trên:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Commons.Controls;assembly=Commons">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Converter/Converter.xaml" />
<ResourceDictionary Source="Styles/Button.xaml" />
<ResourceDictionary Source="BitmapGraphics/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Default Styles -->
<Style TargetType="Button" BasedOn="{StaticResource PrimaryButtonStyle}"></Style>
</ResourceDictionary>
Lưu ý cách tôi xác định kiểu mặc định, được áp dụng tự động, trừ khi bạn chỉ định khác. Trong mỗi cửa sổ hoặc điều khiển, mà bạn muốn tạo kiểu, bạn chỉ cần tham chiếu đến số này ResourceDictionary
. Lưu ý nghĩa của nguồn, mà là một tham chiếu đến assembly (/Commons.Styling;component...
)
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Commons.Styling;component/IncaDesign.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
phong cách mặc định sẽ được thiết lập tự động bây giờ, và nếu bạn muốn truy cập vào một tài nguyên một cách rõ ràng, bạn có thể làm điều này, sử dụng StaticResource
.
<Viewbox Height="16" Width="16" Margin="0,0,10,0">
<ContentControl Content="{StaticResource FileIcon32}" />
</Viewbox>
Đây là giải pháp rất hay theo quan điểm của tôi, giải pháp này được xây dựng với PRISM.
Bạn có thể tạo ResourceDictionary và thêm nó vào một ClassLibrary. Bạn phải tham khảo nó trên App.xaml của bạn và bạn có thể sử dụng Thư viện trong nhiều dự án –