我根据这里在堆栈溢出上找到的代码片段编写了这个脚本,但是在运行时得到了这个错误:
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() )这似乎是解决方案,但不知道我需要修复代码的哪些部分。
以下是两个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> 发布于 2015-08-17 08:14:31
根据msdn,您应该能够使用Window.Show (用于非模态版本)或Window.ShowDialog (如果您希望保持在Revit上下文中)。
Application().Run( MyWindow() )行基本上设置了一个事件循环--这是Revit在启动时已经为您做的事情,因此您可以继续显示您的windows :)
我不能真正测试这个解决方案,因为我没有AboutWindow.xaml和IronPythonWPF.xaml,所以我在这里猜测.去告诉我是怎么回事。
https://stackoverflow.com/questions/32013933
复制相似问题