Next, go to MainPage.xaml.cs, where we will be looking for our script:<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
<script type= "python" > def func(): HtmlPage.Window.Alert( "Hello world!" ) func() </script> * This source code was highlighted with Source Code Highlighter .
Well, the implementation of PythonEngine:* This source code was highlighted with Source Code Highlighter .
- using System.Linq;
- using System.Windows.Browser;
- namespace mygestalt
- {
- public partial class MainPage
- {
- public MainPage ()
- {
- Initializecomponent ();
- FindAndRunScript ();
- }
- private void FindAndRunScript ()
- {
- var scripts = HtmlPage.Document.GetElementsByTagName ( "script" );
- var pythonScript = scripts.Where (x => x.GetProperty ( "type" ) .ToString () == "python" ) .First ();
- PythonEngine.Run (pythonScript.GetProperty ( "innerHtml" ) .ToString ());
- }
- }
- }
Run the application and see:* This source code was highlighted with Source Code Highlighter .
- using Microsoft.Scripting;
- using Microsoft.Scripting.Hosting;
- using Microsoft.Scripting.Silverlight;
- namespace mygestalt
- {
- public static class PythonEngine
- {
- public static ScriptScope Run ( string source)
- {
- var setup = Configuration.LoadFromAssemblies (Package.GetManifestAssemblies ());
- setup.HostType = typeof (BrowserScriptHost);
- setup.DebugMode = true ;
- var runtime = new ScriptRuntime (setup);
- var engine = runtime. GetEngine ( "IronPython" );
- var scope = engine.CreateScope ();
- const string init = @ " import clr clr.AddReference ('System.Windows.Browser') from System.Windows.Browser import *";
- ScriptSource initSource = engine.CreateScriptSourceFromString (init, SourceCodeKind.Statements);
- initSource.Execute (scope);
- var script = engine.CreateScriptSourceFromString (source, SourceCodeKind.Statements);
- script.Execute (scope);
- return scope;
- }
- }
- }
Source: https://habr.com/ru/post/65962/
All Articles