按名称访问窗口
我对 WPF 很陌生,来自 Delphi 世界。我在Delphi世界中解决了下面的问题(尽管很痛苦),并希望在WPF世界中有一个更优雅的解决方案。
我需要读取一个包含菜单“树”的 XML 文件,其中包含窗口名称以及菜单提示,然后能够根据其名称“显示”窗口。
例如,有两个选择的菜单段可能具有如下 XML:
<MenuLeaf>
<Header>Product information</Header>
<MenuLine>
<Prompt>Product Master File</Prompt>
<WindowName>Products.xaml</WindowName>
</MenuLine>
<MenuLine>
<Prompt>Inventory Data</Prompt>
<WindowName>Inventory.xaml</WindowName>
</MenuLine>
</MenuLeaf>
因此,当用户做出“库存数据”选择时,我会知道我想要“显示”窗口 Inventory.xaml 。 ....但我只有文字字符串“Inventory.xaml”。
我将拥有数百个这样的表单,并且 XML 文件可能会不时变化 - 因此,
Dim window as New Inventory
window.Show
为数百个窗口中的每一个窗口提供标准代码对我来说并不有效。
我需要的是
Dim window as New {go out and find the Inventory file with name Inventory.xaml}
window.Show
我无休止地寻找但没有运气的东西。
I am quite new to WPF, coming from the Delphi world. I solved the problem below (albeit painfully) in the Delphi world, and hope there is a more elegant solution in the WPF world.
I need to read in an XML file containing a menu "tree", which has the window names in it as well as the menu prompts, and then be able to "show" a window based on having its name.
For example, a segment of the menu, with two choices, might have XML like this:
<MenuLeaf>
<Header>Product information</Header>
<MenuLine>
<Prompt>Product Master File</Prompt>
<WindowName>Products.xaml</WindowName>
</MenuLine>
<MenuLine>
<Prompt>Inventory Data</Prompt>
<WindowName>Inventory.xaml</WindowName>
</MenuLine>
</MenuLeaf>
So when the user makes the "Inventory Data" choice, I will know that I want to do a "show" of the window Inventory.xaml ..... but I only have the literal string "Inventory.xaml".
I will have hundreds of these forms, and the XML file can vary from time to time - so it's not effective for me to have the standard code of
Dim window as New Inventory
window.Show
for each of the several hundred windows.
What I need is something that does
Dim window as New {go out and find the Inventory file with name Inventory.xaml}
window.Show
I have searched endlessly for this with no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为解决方案的途径是使用反射,这将允许您动态查找/调用您的类。假设您的命名空间是 MyNs,那么其中必须有一个与“Products.xaml”文件对应的“Products”类。要查找它,请使用 MyFoundType = MyNs.GetType("Products")
然后获取此类型的默认(或其他,如果您喜欢)构造函数:MyFoundType.GetConstructor()。然后调用构造函数(如果需要的话可以带参数)-->您现在将窗口作为一个对象。
将其投射到窗口并调用其 Show 方法,就完成了。
http://msdn.microsoft.com/en-us/library/y0cd10tb.aspx
http://msdn.microsoft.com/en-us/library/h93ya84h.aspx
http://msdn.microsoft.com/en-us/library/6ycw1y17.aspx
I think the path to solution is to use Reflection, which will allow you to dynamically find/invoke your classes. Say your Namespace is MyNs, then you must have a 'Products' Class within it that correspond to the 'Products.xaml' file. To find it, use MyFoundType = MyNs.GetType("Products")
Then get default (or other if you like) constructor for this type : MyFoundType.GetConstructor(). Then invoke the constructor (with arguments if needed) --> you now have your window as an Object.
Cast it to a window and call its Show method, and you're done.
http://msdn.microsoft.com/en-us/library/y0cd10tb.aspx
http://msdn.microsoft.com/en-us/library/h93ya84h.aspx
http://msdn.microsoft.com/en-us/library/6ycw1y17.aspx
您需要使用 XamlReader 对象,它在运行时解析 XAML 并创建对象。
XamlReader.Load
将返回 XAML 中指定的实际顶级元素;如果它是一个Window
,您可以.Show
它。如果是其他东西,您需要一个容器来放置它。例如,您可能有一个带有Border
元素的Window
,然后执行以下操作 :如果您实际上不知道 XAML 中元素的类型,您通常可以使用
FrameworkElement
,它是所有可视元素的基类,尽管您不会从中获得特定于 Window 的行为。You need to use the XamlReader object, which parses XAML at run-time and creates the object.
The
XamlReader.Load
will return whatever the actual top-level element in the XAML specifies; if it's aWindow
you can just.Show
it. If it's something else, you'll need a container to place it in. For example, you might have aWindow
with aBorder
element in it and do:If you don't actually know the type of element in your XAML you can usually use
FrameworkElement
, which is the base class for all the visual elements, though you won't get Window-specific behavior from that.