Thursday, January 19, 2012

Ironpython hosting in SilverLight

Here is one of the simplest sample i could make to illustrate.
The only issue i encoutered was adding a reference to 'Microsoft.CSharp'

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Scripting.Silverlight;


///
/// script.py => def Adder (x, y) : return x + y
///


namespace DLRSilverlight
{
    public partial class MainPage : UserControl
    {
        dynamic runtime = DynamicEngine.CreateRuntime().UseFile("script.py");
               
        public MainPage()
        {
            InitializeComponent();                  
        }
       
        private void button1_Click(object sender, RoutedEventArgs e)
        {            
            dynamic result = runtime.Adder(Convert.ToDouble(this.N1.Text), Convert.ToDouble(this.N2.Text));
            MessageBox.Show(String.Format("{0}", result));
        }        
    }
}

No comments:

Post a Comment