使用 MEF 动态构建选项卡式应用程序
我对 MEF 很感兴趣,并计划使用它来构建一个演示应用程序来加载不同的选项卡。我是 MEF 和 WPF 的初学者,尽管 MEF 正在加载程序集,但我仍坚持将控件加载到我创建的 TabItem 中。我的代码看起来像这样..
foreach (var page in pages)
{
TabItem item = new TabItem();
item.Header = page.PageTitle;
/// Errm???
// Add each page
tcPageControl.Items.Add(item);
}
选项卡是页面,所以我可能做错了,任何帮助将不胜感激。
提前致谢
I'm rather taken with MEF and plan to use it to build a demo application to load different tabs. I am a begineer at MEF and WPF and although MEF is loading the assemblies I'm stuck at loading the controls into the TabItem I have created. My code looks a bt like this ..
foreach (var page in pages)
{
TabItem item = new TabItem();
item.Header = page.PageTitle;
/// Errm???
// Add each page
tcPageControl.Items.Add(item);
}
The tabs are Pages, so I might be doing it totally wrong, and any help would be appreciated.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有方便的智能感知,但我认为您需要将项目的内容设置为您的页面。像这样的东西:
Don't have intellisense handy, but I think you need to set the content of the item to be your page. Something like:
这里没有太多细节 - 但基本思想是
[Export]
每个“页面”,可能在一个为您提供页面标题等的自定义类中。然后您可以使用
[ImportMany]
导入页面集合,并为每个导入的页面构建一个“选项卡”。There aren't many details here - but the basic idea would be to
[Export]
each "page", potentially within a custom class that gives you page titles, etc.You'd then use
[ImportMany]
to import a collection of pages, and build a "tab" for each imported page.您只需将 TabItem 的 Content 属性设置为页面,如下所示:
这是使用 LINQ 对其进行编码的更优雅的方法:
一般来说,在 WPF 中,如果您构建自己的 TabItem 结构,则无需编写“foreach”循环。正确编码。
You simply need to set the Content property of your TabItem to be the page, like this:
Here is a much more elegant way to code it using LINQ:
In general in WPF you never have to write a "foreach" loop if you structure your code correctly.
我解决了我的问题,页面只能将框架作为其父级,因此添加此代码可以解决该问题。
I solved my problem, Pages can only have Frames as their parents, so adding this code addresses it.