[英]IronPython WPF Load New Window
Bear with me, I'm new to GUI Programming, IronPython, WPF and .NET. 忍受我,我是GUI编程,IronPython,WPF和.NET的新手。 However, I am fairly familiar with Python.
但是,我对Python相当熟悉。 I have browsed many online tutorials, but none seem to address the exact issue I'm facing.
我浏览了许多在线教程,但似乎都没有解决我所面临的确切问题。 (Perhaps it's trivial? But it is not trivial to someone like me!)
(也许这是微不足道的?但是对像我这样的人来说却并不微不足道!)
Issue: I'd like to know how to launch a new WPF window (XAML) as a new window from my Windows.System.Application. 问题:我想知道如何从Windows.System.Application启动新的WPF窗口(XAML)作为新窗口。 Basically, I want to launch an "About" dialog from the help menu of my app.
基本上,我想从应用程序的帮助菜单中启动“关于”对话框。 I know this can be achieved by using System.Windows.Forms.Form, but in the long run I want to be able to load more complex window arrangements by using XAML markup.
我知道可以通过使用System.Windows.Forms.Form来实现,但是从长远来看,我希望能够通过使用XAML标记来加载更复杂的窗口安排。
Currently, when I click the About Menu (mnuAboutClick) this does load the XAML window, but in the process replaces/destroys the original Main Window (WpfMainWindow). 当前,当我单击“关于菜单”(mnuAboutClick)时,确实会加载XAML窗口,但是在此过程中将替换/破坏原始主窗口(WpfMainWindow)。 I want both windows to remain open.
我希望两个窗口都保持打开状态。
EDIT: Alternatively, is there a way to load the xaml into the System.Windows.Forms.Form? 编辑:或者,有没有办法将xaml加载到System.Windows.Forms.Form中? This would be suitable for my needs.
这将适合我的需求。
Here's an example of my code: 这是我的代码示例:
import wpf
from System.Windows import Application, Window
class MyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'WpfMainWindow.xaml')
def mnuAboutClick(self, sender, e):
print 'About Menu Click'
wpf.LoadComponent(self, 'WpfAboutWindow.xaml') # How to launch "About Dialog", This works, but destroys "WpfMainWindow"!
if __name__ == '__main__':
Application().Run(MyWindow())
If you want to show two window in the same time you should use Show
( msdn ) or ShowDialog
( msdn ) method. 如果要同时显示两个窗口,则应使用
Show
( msdn )或ShowDialog
( msdn )方法。
Example: 例:
File "AboutWindow.xaml": 文件“ AboutWindow.xaml”:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AboutWindow" Height="300" Width="300">
<Grid>
<TextBlock Text="AboutWindow" />
</Grid>
</Window>
File "AboutWindow.py": 文件“ AboutWindow.py”:
import wpf
from System.Windows import Window
class AboutWindow(Window):
def __init__(selfAbout):
wpf.LoadComponent(selfAbout, 'AboutWindow.xaml')
File "IronPythonWPF.xaml": 文件“ IronPythonWPF.xaml”:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IronPythonWPF" Height="300" Width="300">
<StackPanel>
<Menu>
<MenuItem Header="About" Click="MenuItem_Click" />
</Menu>
<TextBlock Text="MainWindow" />
</StackPanel>
</Window>
File "IronPythonWPF.py": 文件“ IronPythonWPF.py”:
import wpf
from System.Windows import Application, Window
from AboutWindow import *
class MyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'IronPythonWPF.xaml')
def MenuItem_Click(self, sender, e):
form = AboutWindow()
form.Show()
if __name__ == '__main__':
Application().Run(MyWindow())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.