如何动态加载XAML以获取控件信息

发布于 2025-01-03 07:30:43 字数 837 浏览 2 评论 0原文

我正在尝试加载一个 Silverlight 项目来读取每个 XAML 文件,方法是使用每个 XAML 类的反射 Activator.CreateInstance 创建一个实例读取其控件。

C# 代码:

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"
}

也许,反射无法识别 Silverlight 样式 ComboBoxStyle。我如何创建一个实例来动态读取 XAML 文件中的每个控件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2025-01-10 07:30:43

经过与谷歌的斗争后,我终于找到了解决问题所需的方法。

  1. 复制来自Silverlight项目的所有样式资源(打算加载)。
  2. 将它们粘贴到主/调用者 Silverlight 项目或应用程序的 App.xaml 中,该项目或应用程序使用反射代码加载 Silverlight 控件信息

遵循这些步骤将消除缺少样式XAML解析异常

找不到具有名称/键 ComboBoxStyle 的资源

参考的资源: 创建表单实例时,XAML 解析器无法在动态加载的 XAP 中找到资源

I have managed to find the required solution to my problem after struggling with the Google.

  1. Copy all the Style Resources from Silverlight project (intended to load).
  2. Paste them in the 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

落花浅忆 2025-01-10 07:30:43

我能够使用 XamlReader 类加载自定义控件。
我使用的是包含控件的 XAML 的纯字符串,与您的反射想法不同。

//string xaml = "<...>";
var content = XamlReader.Load(xaml) as FrameworkElement;
this.scrollViewer.Content = content;

XamlReader 类型位于 System.Windows.Markup 中。

如果您的情况可能,您可以尝试从程序集中获取 XAML 资源并将它们读取为字符串。然后使用提供的代码。获得 content 变量后,您可以使用 Silverlight API 对控件执行任何操作。
希望这会对您有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文