Created
March 27, 2009 06:46
-
-
Save jschementi/86575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the following methods to the calculator's | |
// Page.xaml.cs Page class | |
// Execute the script code in the Functions buffer, | |
// and add buttons to the UI to call the function | |
// if it doesn't exist. | |
public void Functions_TextChanged(object sender, TextChangedEventArgs e) { | |
FunctionDefinitions.Children.Clear(); | |
try { | |
object result = _engine.Execute(Functions.Text.ToString()); | |
foreach(var method in _engine.ListOfMethods()) { | |
if (!method.StartsWith("__")) { | |
var b = new Button(); | |
b.Tag = method; | |
b.Content = method + "(x)"; | |
b.Click += new RoutedEventHandler(RunCustomFunction); | |
FunctionDefinitions.Children.Add(b); | |
} | |
} | |
} catch (Exception) { | |
// Ignore | |
} | |
} | |
// Call a user-defined function | |
public void RunCustomFunction(object sender, RoutedEventArgs e) { | |
try { | |
object result = _engine.CallMethod((string)((Button)sender).Tag, (object)Decimal.Parse(Calculator.CurrentNumber.ToString())); | |
Calculator.CurrentNumber = new NumberProject.Number(result.ToString()); | |
Calculator.OnValueChanged(); | |
} catch (Exception) { | |
HtmlPage.Window.Alert("The user-defined function returned None"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To hook up the above methods, add a PythonEngine | |
// field to the Page class | |
private PythonEngine _engine; | |
// And in the Page constructor, initialize the | |
// engine and hook the Functions.KeyUp event | |
_engine = new PythonEngine(); | |
Functions.TextChanged += new TextChangedEventHandler(Functions_TextChanged); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment