Reputation: 16698
I am trying to load a Silverlight project to read every XAML
file by creating an instance using reflection, Activator.CreateInstance
, of every XAML
class for reading its controls.
C# Code:
string strPath = "SilverlightUI.dll";
StreamResourceInfo sri = Application.GetResourceStream(new Uri(strPath, UriKind.RelativeOrAbsolute));
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(sri.Stream);
Type[] typeArray = assembly.GetExportedTypes();
foreach (Type type in typeArray)
{
object ctl = (object)Activator.CreateInstance(type);
// Following exception is occurring while creating an instance using above line of code
// Exception "Cannot find a Resource with the Name/Key ComboBoxStyle"
}
Perhaps, reflection is not able to recognize Silverlight style ComboBoxStyle
. How can i possibly create an instance to read every control in the XAML file dynamically?
Upvotes: 2
Views: 2125
Reputation: 16698
I have managed to find the required solution to my problem after struggling with the Google.
App.xaml
of the Master/Caller Silverlight project or application, which is using the reflection code to load the Silverlight Controls information Following these steps will eliminate the XAML Parse Exception of missing Style.
Cannot find a Resource with the Name/Key ComboBoxStyle
Reference: XAML Parser cannot find resource within dynamically loaded XAP when creating form instance
Upvotes: 2
Reputation: 951
I was able to load custom controls using the XamlReader
class.
I am using plain string that contains the XAML of the control not like your reflection idea.
//string xaml = "<...>";
var content = XamlReader.Load(xaml) as FrameworkElement;
this.scrollViewer.Content = content;
The type XamlReader
is in System.Windows.Markup
.
If it is possible in your case you can try to get XAML resources from your assembly and read them to string. Then use the provided code. After you have the content
variable you can do anything using the Silverlight API to the control.
Hope this will help you.
Upvotes: -1