在运行时加载嵌入在 DLL 中的 WPF 控件

发布于 2024-12-11 07:29:14 字数 224 浏览 0 评论 0原文

在我的 WPF 项目中,我有一个包含多个 WPF 用户控件的 dll。我希望在运行时能够检查数据库中的参数(已实现)并根据该参数(这是一个字符串)能够将特定的 UserControl 加载到我的视图中。

UserControl实际上是一个Canvas,所以它基本上只是根据数据库条目将正确的Canvas放置在View上。

不知道我说清楚了没有,如果有不懂的可以问我。

感谢所有帮助者!

In my WPF project, I have a dll that contains several WPF UserControls. I would like, in runtime, to be able to check a parameter in the database (already implemented) and according to that parameter (which is a string) to be able to load a specific UserControl to my View.

The UserControl is actually a Canvas, so it basically just places the correct Canvas on the View according to the database entry.

I don't know if I was clear, so please ask me if you didn't understand the question.

Thanks to all helpers!

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

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

发布评论

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

评论(1

少跟Wǒ拽 2024-12-18 07:29:14

这种在运行时从 dll 加载控件或类似内容的概念称为反射这是在某些情况下的常见处理方式。尝试用 google 搜索 Reflection in C# 你会发现很多关于它的教程。

基本上你会在运行时加载 dll。然后你就会寻找控制权。一旦找到它,您将创建它的实例并使用它。所有这些都将在运行时发生

  UserControl myControl = null;    
  Assembly asm = Assembly.LoadFile(Your dll path);
  Type[] tlist = asm.GetTypes();
  foreach (Type t in tlist)
  {
     if(t.Name == "Your class name" )
     {
         myControl = Activator.CreateInstance(t) as UserControl;
         break;
     }               
  }

另请参阅这个问题供参考

This concept of loading controls or similar things from a dll at runtime is called Reflection and it is a common way of doing things in certain scenarios. Try to google Reflection in C# you will find a lot of tutorials about it.

Basically you will load the dll at runtime. Then you will look for control. Once you find it you will create its instance and use it. All this will happen at runtime

  UserControl myControl = null;    
  Assembly asm = Assembly.LoadFile(Your dll path);
  Type[] tlist = asm.GetTypes();
  foreach (Type t in tlist)
  {
     if(t.Name == "Your class name" )
     {
         myControl = Activator.CreateInstance(t) as UserControl;
         break;
     }               
  }

Also see this question for reference

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