Create a custom control (templated control) in Silverlight. A themes folder is generated under the project and contains a generic. XAML file. This is a resourcedictionary file. The default style of all custom controls must be placed here.
The most primitive method is to write all styles directly in the generic. XAML file. However, if there are enough custom controls and generic. XAML Reaches thousands of lines, it is of course very troublesome to manage them. Later, at the suggestion of my colleagues, I got two ways to manage the styles of the custom controls separately, which finally solved this headache.
Mergedefaultstyle Method
If you have studiedSource codeYou will find that all the custom controls have a separate XAML file to save the default style of the control. Of course, these files do not work. Initially I thought it was to write the control style with a separate XAML file, and then copy it to generic. XAML, that is, manual synchronization. So I did this ...... In the end, it was silly and naive to find that manual synchronization is less reliable than the Dropbox on the wall.
Later, we found mergedefaultstyle to figure out that it was previously played.
Mergedefaultstyle is used to apply a special build method to all individual XAML files. During the build project, the content of the XAML file is automatically integrated into generic. XAML.
See: http://www.jeff.wilcox.name/2009/01/default-style-task/ for details
Key steps:
1. CopyCodeOr download mergedefaultstyle. dll directly.
2. Unload your project in Vs, edit the project file, or directly open the csproj file in a text editor.
3. Add the following code at the end:
<UsingtaskTaskname="Engineering. Build. Tasks. mergedefastystylestask"Assemblyfile="$ (Engineeringresources) \ engineering. Build. dll"/>
Note: The value of assemblyfile is the location where you put mergedefaultstyle. dll. You can use the relative path.
4. Add the code below:
<! -- Add "defaultstyle" as a build action in Visual Studio --> < Itemgroup Condition = " '$ (Buildinginsidevisualstudio)' = 'true' " > < Availableitemname Include = " Defaultstyle " /> </ Itemgroup > <! -- Merge the default styles of controls (only if any of the defaultstyle files ismore recent than the project's generic. XAML file) before compilationdependencies are processed. --> < Propertygroup > < Prepareresourcesdependson > Mergedefastystyles; $ (prepareresourcesdependson ); </ Prepareresourcesdependson > </ Propertygroup > < Target Name = " Mergedefastystyles " Inputs = " @ (Defastyle style) " Outputs = " $ (Msbuildprojectdirectory) \ Generic. XAML " > < Mergedefastystylestask Defaultstyles = " @ (Defastyle style) " Projectdirectory = " $ (Msbuildprojectdirectory) " /> </ Target > <! -- Touch defaultstyles on rebuild to force generation of generic. XAML. --> < Propertygroup > < Rebuilddependson > Touchdefastystyles; $ (rebuilddependson ); </ Rebuilddependson > </ Propertygroup > < Target Name = " Touchdefastystyles " > < Touch Files = " @ (Defastyle style) " Forcetouch = " True " /> </ Target >
5. Load your project again.
6. Select a separate XAML with the default style, and select defaultstyle In the build action in the Property Window.
7. Compile the entire project and open the generic. XAML file. You will find that the content in the XAML file has been copied to generic. XAML.
This method applies to Silverlight 2 \ 3 \ 4.
Mergeddictionary Method
The above method is once and for all, but it is somewhat unofficial. In addition, it is still generic. XAML that controls the global control. Once a XAML file is leaked, it will affect all controls followed by errors. Troubleshooting is also troublesome.
So in Silverlight 3, a simpler and more official method is provided. As mentioned above, the generic. XAML file contains a resourcedictionary, while resourcedictionary in Silverlight 3 has a mergeddictionaries attribute while other resourcedictionary can be integrated into a resourcedicionary through the resource path.
In fact, when creating a Silverlight navigation application, you can see the application of this attribute in APP. XAML. Note that the relative path can be used in APP. XAML,In generic. XAML, the relative path cannot be used, but the file path should be described using the "/assemblyname; component/path" method.
For example, the assemblyname of your project is slippor. controls, and The XAML path is customcontrol. XAML in the customcontrol folder. It should be written in generic. XAML as follows:
< Resourcedictionary Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml " Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation " > < Resourcedictionary. mergeddictionaries > < Resourcedictionary Source = " /Slippor. controls; component/customcontrol. XAML "/> </ Resourcedictionary. mergeddictionaries > </ Resourcedictionary >
This method applies to Silverlight 3 \ 4.