3

I've written this script based on the snippets I found here on stackoverflow but get this error at runtime:

System.InvalidOperationException: Cannot create more than one System.Windows.Application instance in the same AppDomain.

I know it's got something to do with the fact that the last statement is creating a new Application instance within the same AppDomain but I don't know how to fix this. Here is the script:

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 seems to be the solution but don't know what parts of this code I would need to fix mine.

Here is the contents for the two XAML files:

__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> 
Community
  • 1
  • 1
Ehsan Iran-Nejad
  • 1,697
  • 1
  • 15
  • 20

1 Answers1

3

According to msdn, you should be able to use Window.Show (for a modeless version) or Window.ShowDialog (if you want to stay in the Revit API context)

The Application().Run( MyWindow() ) line basically sets up an event loop - something Revit already did for you at startup, so you can just go ahead and show your windows :)

I can't really test this solution since I don't have AboutWindow.xaml and IronPythonWPF.xaml, so I'm just guessing here... Have a go and tell me how it went.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • Okay so I changes the last line to: MyWindow().Show() but now it's tellimg me "LoadComponent() takes exactly 3 arguments (2 given)"...It wants to use the LoadComponent(Object, Uri) version of the function for some reason. – Ehsan Iran-Nejad Aug 17 '15 at 14:49
  • actually it seems all the LoadComponent() overloads in wpf want a CodeContext object as the first parameter...:| ...I'm stuck. – Ehsan Iran-Nejad Aug 18 '15 at 06:19
  • 1
    huh... it works for me... proof: https://www.dropbox.com/s/uwpz05o08m5at58/2015-08-18%2012_27_25-AboutWindow.png?dl=0, https://www.dropbox.com/s/wtfkz8fmh4i947v/2015-08-18%2012_26_30-IronPythonWPF.png?dl=0 – Daren Thomas Aug 18 '15 at 10:29
  • Hmm...Okay I'll try agajn. Did u run this from RPS shell window? – Ehsan Iran-Nejad Aug 18 '15 at 14:06
  • 2
    no - i created an external script to run your code... i wonder if it could be an issue of the .NET version RPS is linked to. if i remember correctly, it changes from RPS 2015 to RPS 2016... – Daren Thomas Aug 26 '15 at 08:13
  • Do you think it might have something to do with the way I'm referencing the IronPython WPF library? I don't have a standalone IronPython installed on my machine right now but I'm using the DLLs that I copied from a previous IronPython installation (platforms folder)....Do I need to have a standalone IronPython installed?? `folder = os.path.dirname( __file__ )` `sys.path.append( os.path.join( folder, 'platforms\\Net45' ))` – Ehsan Iran-Nejad Aug 26 '15 at 15:51
  • I don't really know. I remember moving the IronPython.Wpf dll to the GAC. Maybe that will help? – Daren Thomas Aug 31 '15 at 14:06