2013-07-06 106 views
4

忍受我,我是GUI編程,IronPython,WPF和.NET的新手。但是,我對Python很熟悉。我瀏覽了許多在線教程,但似乎沒有解決我面臨的確切問題。 (或許它是微不足道的,但對像我這樣的人來說這不是微不足道的)IronPython WPF加載新窗口

問題:我想知道如何從Windows.System.Application作爲新窗口啓動一個新的WPF窗口(XAML)。基本上,我想從我的應用程序的幫助菜單啓動一個「關於」對話框。我知道這可以通過使用System.Windows.Forms.Form來實現,但從長遠來看,我希望能夠使用XAML標記加載更復雜的窗口布局。

當前,當我點擊關於菜單(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()) 

回答

6

如果你想在同一時間顯示兩個窗口,你應該使用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()) 
+1

這給我的眼睛帶來了淚水。謝謝。如此美麗。 – James

+0

你知不知道,如果這也有可能,如果我只有一個網格,我想在現有的窗口嵌入? – BendEg