我根据我在 stackoverflow 上找到的代码片段编写了此脚本,但在运行时出现此错误:
System.InvalidOperationException:无法创建多个
同一 AppDomain 中的 System.Windows.Application 实例。
我知道这与最后一个语句在同一个 AppDomain 中创建一个新的应用程序实例有关,但我不知道如何解决这个问题。
这是脚本:
clr.AddReference('PresentationCore')
clr.AddReference("PresentationFramework")
clr.AddReference('Microsoft.Dynamic')
clr.AddReference('Microsoft.Scripting')
clr.AddReference('System')
clr.AddReference('IronPython')
clr.AddReference('IronPython.Modules')
clr.AddReference('IronPython.Wpf')
from System.Windows import Application, Window
from IronPython.Modules import Wpf as wpf
class AboutWindow(Window):
def __init__(selfAbout):
wpf.LoadComponent( selfAbout, os.path.join( folder, 'AboutWindow.xaml' ))
class MyWindow(Window):
def __init__(self):
wpf.LoadComponent( self, os.path.join( folder, 'IronPythonWPF.xaml' ))
def MenuItem_Click(self, sender, e):
form = AboutWindow()
form.ShowDialog()
if __name__ == '__main__':
Application().Run( MyWindow() )
This https://stackoverflow.com/questions/8047610/re-open-wpf-window-from-a-console-application似乎是解决方案,但不知道我需要修复该代码的哪些部分。
以下是两个 XAML 文件的内容:
__WIP__wpfTest__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>
__WIP__wpfTest__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>