我正在尝试将IronPython与monodroid一起使用(使用Android 4手机),这样我就可以在我的项目中执行python脚本。我的代码基于此处的示例:Instantiating a python class in C#
我已将dll包含在Android平台目录中(使用IronPython-2.7.2rc1)并在项目目录中有一个python脚本(现在是一个名为Calculator的测试脚本,从该示例链接)
以下是我的C#代码:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace PythonTest
{
[Activity (Label = "PythonTest", MainLauncher = true)]
public class Activity1 : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile("Calculator.py");
ScriptScope scope = engine.CreateScope();
ObjectOperations op = engine.Operations;
source.Execute(scope); // class object created
object klaz = scope.GetVariable("Calculator"); // get the class object
object instance = op.Invoke(klaz); // create the instance
object method = op.GetMember(instance, "add"); // get a method
int result = (int)op.Invoke(method, 4, 5); // call method and get result (9)
var aLabel = new TextView (this);
//aLabel.Text = result.ToString();
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++); };
}
}
}
它构建没有错误。当我尝试在手机上运行时,我收到这些错误并且程序崩溃(手机返回主屏幕)
I/monodroid-gc( 2110): environment supports jni NewWeakGlobalRef
I/MonoDroid( 2110): UNHANDLED EXCEPTION: System.NotImplementedException: The requested feature is not implemented.
I/MonoDroid( 2110): at Microsoft.Scripting.PlatformAdaptationLayer.OpenInputFileStream (string) <0x0001c>
I/MonoDroid( 2110): at Microsoft.Scripting.FileStreamContentProvider/PALHolder.GetStream (string) <0x0002b>
I/MonoDroid( 2110): at Microsoft.Scripting.FileStreamContentProvider.GetStream () <0x00023>
I/MonoDroid( 2110): at Microsoft.Scripting.Runtime.LanguageBoundTextContentProvider.GetReader () <0x00027>
I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.GetReader () <0x00023>
I/MonoDroid( 2110): at IronPython.Compiler.Parser.CreateParserWorker (Microsoft.Scripting.Runtime.CompilerContext,IronPython.PythonOptions,bool) <0x000bf>
I/MonoDroid( 2110): at IronPython.Compiler.Parser.CreateParser (Microsoft.Scripting.Runtime.CompilerContext,IronPython.PythonOptions) <0x0001f>
I/MonoDroid( 2110): at IronPython.Runtime.PythonContext.ParseAndBindAst (Microsoft.Scripting.Runtime.CompilerContext) <0x0005f>
I/MonoDroid( 2110): at IronPython.Runtime.PythonContext.CompilePythonCode (Microsoft.Scripting.SourceUnit,Microsoft.Scripting.CompilerOptions,Microsoft.Scripting.ErrorSink) <0x0008f>
I/MonoDroid( 2110): at IronPython.Runtime.PythonContext.CompileSourceCode (Microsoft.Scripting.SourceUnit,Microsoft.Scripting.CompilerOptions,Microsoft.Scripting.ErrorSink) <0x0002b>
I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.Compile (Microsoft.Scripting.CompilerOptions,Microsoft.Scripting.ErrorSink) <0x0005b>
I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope,Microsoft.Scripting.ErrorSink) <0x00057>
I/MonoDroid( 2110): at Microsoft.Scripting.SourceUnit.Execute (Microsoft.Scripting.Runtime.Scope) <0x00027>
I/MonoDroid( 2110): at Microsoft.Scripting.Hosting.ScriptSource.Execute (Microsoft.Scripting.Hosting.ScriptScope) <0x00043>
I/MonoDroid( 2110): at PythonTest.Activity1.OnCreate (Android.OS.Bundle) <0x000c3>
I/MonoDroid( 2110): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x00057>
I/MonoDroid( 2110): at (wrapper dynamic-method) object.5a9608db-a03f-4bd2-831d-6cb21e250354 (intptr,intptr,intptr) <0x00033>
E/mono ( 2110):
E/mono ( 2110): Unhandled Exception: System.NotImplementedException: The requested feature is not implemented.
E/mono ( 2110): at Microsoft.Scripting.PlatformAdaptationLayer.OpenInputFileStream (System.String path) [0x00000] in <filename unknown>:0
E/mono ( 2110): at Microsoft.Scripting.FileStreamContentProvider+PALHolder.GetStream (System.String path) [0x00000] in <filename unknown>:0
E/mono ( 2110): at Microsoft.Scripting.FileStreamContentProvider.GetStream () [0x00000] in <filename unknown>:0
E/mono ( 2110): at Microsoft.Scripting.Runtime.LanguageBoundTextContentProvider.GetReader () [0x00000] in <filename unknown>:0
E/mono ( 2110): at Microsoft.Scripting.SourceUnit.GetReader () [0x00000] in <filename unknown>:0
E/mono ( 2110): at IronPython.Compiler.Parser.CreateParserWorker (Microsoft.Scripting.Runtime.CompilerContext context, IronPython.PythonOptions options, Boolean verbatim) [0x00000] in <filename unknown>:0
E/mono ( 2110): at IronPython.Compiler.Parser.CreateParser (Microsoft.Scripting.Runtime.CompilerContext context, IronPython.PythonOptions options) [0x00000] in <filename unknown>:0
E/mono ( 2110): at IronPython.Ru
有谁知道如何解决这个问题?谢谢。
答案 0 :(得分:0)
看起来像一个直接的错误(Android的PAL不完整),看起来你已经打开了一个问题(谢谢!)。
IronPython的支持很早,因此测试和报告问题的人越多越好。