In one of the previous WPF projects, because the settings control template failed to read in the foreground XAML, the idea was to read the template in a reflection form, first writing the template to an XML file, and then reading the XML file, here first: the difference between resources and embedded resources, embedding resources ( Embedded Resource)-embeds the file as a DLL or executable in the main project build output, and can be published as part of the assembly by setting. NET uses resource files as part of an assembly to use external resources in a common way. Resource files are also easier to read, strings, pictures, and any binary data, including any type of file that can be used as a resource item. The following describes how two files are read.
Read about resource files/Embedded Resource files
The general way to read a resource file/Embedded resource file is to load the assembly where the resource resides, using reflection to get the external file data in the assembly:
1 Read resource file name using: string[] assembly.getmanifestresourcenames (). Returns a list of all the assembly resources
2 Read the file resource file using the System.Resources.ResourceManager class, constructor signature: public ResourceManager (String baseName, Assembly Assembly)
3 Read the embedded resource file using Assembly.GetManifestResourceStream (string name)
The following will be the foreground of the XML file and code to describe the reading method.
In our definition of embedded Resource animationtemplate.xml, the specific content is as follows:
<controltemplate
xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "http:/ /schemas.microsoft.com/winfx/2006/xaml "
xmlns:esri=" http://schemas.esri.com/arcgis/client/2009 ";
< Canvas>
<visualstatemanager.visualstategroups>
<visualstategroup x: Name= "CommonStates";
<visualstate x:name= "Normal";
<storyboard repeatbehavior= "Forever",
<doubleanimation BeginTime = "0"
storyboard.targetname= "Ellipse" story Board. Targetproperty= "RenderTransform. (Scaletransform.scalex) "
from=" 1 "to=" D " uration= "00:00:01"/>
<doubleanimation begintime= "0"
Storyboard.targetname= "Ellipse" storyboard.targetproperty= "RenderTransform. (Scaletransform.scaley) "
from= "1" to= "duration=" 00:00:01 "/>
<doubleanimation begintime= "0"
Storyboard.targetname= "Ellipse" storyboard.targetproperty= "(uielement.opacity)"
from= "1" to= "0" duration= "00:00:01"/>
</Storyboard>
</VisualState>
<visualstate x:name= "MouseOver"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ellipse height= "width=" "canvas.left="-8 "canvas.top="-8 "
Rendertransformorigin= "0.5,0.5" x:name= "Ellipse"
ishittestvisible= "False" >
<Ellipse.RenderTransform>
<scaletransform/>
</Ellipse.RenderTransform>
<Ellipse.Fill>
<RadialGradientBrush>
<gradientstop color= "#00FF0000"/>
<gradientstop color= "#FFFF0000" offset= "0.25"/>
<gradientstop color= "#00FF0000" offset= "0.5"/>
<gradientstop color= "#FFFF0000" offset= "0.75"/>
<gradientstop color= "#00FF0000" offset= "1"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<ellipse height= "width=" "canvas.left="-8 "canvas.top="-8 "
Fill= "#FFFF0000" x:name= "Ellipse1"/>
</Canvas>
</ControlTemplate>
The background reads the following way:
Assembly-assembly.getexecutingassembly ();//Gets the assembly that contains the code that is currently executing.
System.IO.UnmanagedMemoryStream ums =. GetManifestResourceStream ("Gqypgis. Xml.AnimationTemplate.xml ") as system.io.unmanagedmemorystream;//Embedded Resource file read, here Unmanagedmemorystream provides the ability to access unmanaged memory blocks from managed code.
byte[] bytes = new Byte[ums. Length];
UMS. Read (bytes, 0, (int) ums. Length);
String xmlstring = System.Text.Encoding.UTF8.GetString (bytes);//decodes the bytes in the specified byte array into a string
XMLString = System.Text . RegularExpressions.Regex.Replace (xmlstring, "^[^<]", "");
byte[] datas = System.Text.Encoding.ASCII.GetBytes (xmlstring);
System.IO.MemoryStream ms = new MemoryStream (datas);
Markersymbol symbol = new Markersymbol ();
symbol. ControlTemplate = (ControlTemplate) System.Windows.Markup.XamlReader.Load (MS);//reads the XAML input from the specified stream , and returns the object as the root of the corresponding object .
Also describe the types and meanings of common build actions:
Content-do not compile the file, but include it in the content output group.
Compile (Compile)-compiles the file into the build output. This setting is used for code files.
Embedded Resource (Embedded Resource)-embeds the file as a DLL or executable file in the main project build output.
To read XML-formatted ControlTemplate by reflection