IronPython WPF加载新风

2024-09-19 21:02:21 发布

您现在位置:Python中文网/ 问答频道 /正文

请原谅,我对GUI编程、IronPython、WPF和.NET都是新手。不过,我对Python相当熟悉。我浏览了很多在线教程,但似乎没有一个能解决我所面临的问题。(也许是小事?但对我这样的人来说,这并不是小事

问题:我想知道如何从我的Windows.System.Application启动一个新的WPF窗口(XAML)。基本上,我想从我的应用程序的“帮助”菜单启动一个“关于”对话框。我知道这可以通过使用System.Windows.Forms.Form实现,但从长远来看,我希望能够通过使用XAML标记加载更复杂的窗口排列。

目前,当我单击About菜单(mnuAboutClick)时,这会加载XAML窗口,但在这个过程中会替换/销毁原始主窗口(WpfMainWindow)。我要两扇窗户都开着。

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

下面是我的代码示例:

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

Tags: selfformapplicationwindows菜单formssystemabout
1条回答
网友
1楼 · 发布于 2024-09-19 21:02:21

如果要同时显示两个窗口,则应使用Showmsdn)或ShowDialogmsdn)方法。

示例:

文件“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>

文件“AboutWindow.py”:

import wpf

from System.Windows import Window

class AboutWindow(Window):
    def __init__(selfAbout):        
        wpf.LoadComponent(selfAbout, 'AboutWindow.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> 

文件“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())

相关问题 更多 >