Runtime Environment: Silverlight 3.0 ( I did not perform tests under 2.0. It seems that this method does not support 2.0, because resourcedictionary does not have the source attribute under 2.0)
Resourcedictionary Resources = New Resourcedictionary ();
Resources. Source = New Uri ( " /Cinlap. UI. SL. controls; component/themes/generic. XAML " , System. urikind. Relative );
Controltemplate=Resources [templatename]AsControltemplate;
As long as the resourcedictionary. Source attribute is set to a suitable Uri, you can use the key to obtain the desired object.
Added: Another scenario using resourcedictionary was just found.
When creating a custom control, we may need. defining the styles of multiple controls in XAML makes it very difficult to find out more definitions. If it is defined in a single file, it is then defined in generic. it is not good to include data in XAML. You can see telerik. redcontrol for Silverlight used <? Include (***. XAML)?> But I have no idea how to implement this function. Instead, I had to find another method. Finally, I found that the same function can be implemented using resourcedictionary. Source.CodeAs follows:
< Resourcedictionary
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml" >
<Resourcedictionary. mergeddictionaries>
<ResourcedictionarySource= "/Cinlap. UI. SL. controls; component/themes/dockpanel. XAML"/>
</Resourcedictionary. mergeddictionaries>
</Resourcedictionary>
Obviously, this method is also used to include another XAML file in a XAML file.