Before the ResourceDictionary theme resource replacement (a) before the resources to cover the topic by loading sequence , introduces the WPF framework for merger rules ResourceDictionary resources.
Cipian scheme introduced during compilation, resource replacement
Foreword
The following diagram, both present in the project theme resource dictionary, we want to modify the configuration of a key theme of the project.
First, we are using the default gray theme
Add Project properties file
New File Themes.props (Theme Settings),
The current version of the theme and added:
1 <Project> 2 <PropertyGroup> 3 <Theme>Theme-Red</Theme> 4 </PropertyGroup> 5 </Project>
As above, switch the current version of the red theme
Modify the project csproject
Introducing the theme configuration file:
<Import Project="..\Themes.props" Condition="Exists('..\Themes.props')" />
Adding resources to replace:
Before compiling, determine the current theme, if the theme is red, it is replaced;
- The file Theme-Normal.xaml project, Theme-Red.xaml Remove all (if Theme-Red.xaml not added to the project, based on the local file exists, you do not need to delete this step)
- The Theme-Red.xaml disguised as the current Theme-Normal.xaml program resources
1 <Target Name="ReplaceTheme" BeforeTargets="BeforeBuild" Condition="'$(Theme)' == 'Theme-Red'"> 2 <ItemGroup> 3 <Page Remove="Theme-Normal.xaml" /> 4 <Page Remove="Theme-Red.xaml" /> 5 <Page Include="Theme-Red.xaml"> 6 <Generator>MSBuild:Compile</Generator> 7 <Link>Theme-Normal.xaml</Link> 8 </Page> 9 </ItemGroup> 10 </Target>
After replacing the theme color:
Need to pay attention to the pit
This program requires resources in key matches the corresponding dictionary, if missing a certain style and other resources, the follow-up may have unexpected abnormal oh ~
How to avoid?
Resource dictionary can be downloaded repair tool, Github Address: ResourceIntegrityCheck
Tool will lack the resources to copy this resource file from another resource dictionary. It has been confirmed so before the repair is completed, submit the code, resources such as style matches the current theme.