IronPython WPF 加载新窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17504787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:10:16  来源:igfitidea点击:

IronPython WPF Load New Window

pythonwpfwindowsuser-interfaceironpython

提问by James

Bear with me, I'm new to GUI Programming, IronPython, WPF and .NET. However, I am fairly familiar with 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!)

请耐心等待,我是 GUI 编程、IronPython、WPF 和 .NET 的新手。但是,我对 Python 相当熟悉。我浏览了许多在线教程,但似乎没有一个能解决我面临的确切问题。(也许是微不足道的?但对于我这种人来说,可不是微不足道的!)

Issue: I'd like to know how to launch a new WPF window (XAML) as a new window from my Windows.System.Application. 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.

问题:我想知道如何从我的 Windows.System.Application 启动一个新的 WPF 窗口 (XAML) 作为一个新窗口。基本上,我想从我的应用程序的帮助菜单中启动一个“关于”对话框。我知道这可以通过使用 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). I want both windows to remain open.

目前,当我单击关于菜单 (mnuAboutClick) 时,这会加载 XAML 窗口,但在此过程中会替换/破坏原始主窗口 (WpfMainWindow)。我希望两个窗口都保持打开状态。

EDIT: Alternatively, is there a way to load the xaml into the System.Windows.Forms.Form? This would be suitable for my needs.

编辑:或者,有没有办法将 xaml 加载到 System.Windows.Forms.Form 中?这将适合我的需要。

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())

回答by kmatyaszek

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())