Permalink
Browse files

Add IronPython sample.

  • Loading branch information...
1 parent a2dec27 commit 3d56e71ad76883f4594c7e7dc5ea7ab4970dd988 @jdhardy committed Jun 11, 2012
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F97F76A2-D298-488B-93B0-C4F93B63ECA5}</ProjectGuid>
+ <ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>AndroidSample</RootNamespace>
+ <AssemblyName>AndroidSample</AssemblyName>
+ <FileAlignment>512</FileAlignment>
+ <AndroidApplication>true</AndroidApplication>
+ <AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
+ <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <AndroidUseSharedRuntime>True</AndroidUseSharedRuntime>
+ <AndroidLinkMode>None</AndroidLinkMode>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
+ <AndroidLinkMode>SdkOnly</AndroidLinkMode>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
+ <HintPath>bin\Debug\IronPython.dll</HintPath>
+ </Reference>
+ <Reference Include="IronPython.Modules, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
+ <HintPath>bin\Debug\IronPython.Modules.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
+ <HintPath>bin\Debug\Microsoft.Dynamic.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Scripting, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL">
+ <HintPath>bin\Debug\Microsoft.Scripting.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Scripting.Core, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL" />
+ <Reference Include="Mono.Android" />
+ <Reference Include="mscorlib" />
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Numerics" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="FlippingGameActivity.cs" />
+ <Compile Include="Resources\Resource.Designer.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <AndroidAsset Include="..\FlippingGame\FlippingGame.py">
+ <Link>Assets\FlippingGame\FlippingGame.py</Link>
+ </AndroidAsset>
+ <AndroidAsset Include="Assets\FlippingGameActivity.py" />
+ <None Include="Resources\AboutResources.txt" />
+ <None Include="Assets\AboutAssets.txt" />
+ </ItemGroup>
+ <ItemGroup>
+ <AndroidResource Include="Resources\Layout\Main.axml" />
+ </ItemGroup>
+ <ItemGroup>
+ <AndroidResource Include="Resources\Values\Strings.xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <AndroidResource Include="Resources\Drawable\Icon.png" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,19 @@
+Any raw assets you want to be deployed with your application can be placed in
+this directory (and child directories) and given a Build Action of "AndroidAsset".
+
+These files will be deployed with you package and will be accessible using Android's
+AssetManager, like this:
+
+public class ReadAsset : Activity
+{
+ protected override void OnCreate (Bundle bundle)
+ {
+ base.OnCreate (bundle);
+
+ InputStream input = Assets.Open ("my_asset.txt");
+ }
+}
+
+Additionally, some Android functions will automatically load asset files:
+
+Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
@@ -0,0 +1,22 @@
+from AndroidSample import Resource
+
+class FlippingGameActivity(object):
+ def __init__(self, main):
+ self.main = main
+ self.count = 1
+
+ def OnCreate(self, bundle):
+ # Set our view from the "main" layout resource
+ self.main.SetContentView(Resource.Layout.Main);
+
+ self.resultLabel = self.main.FindViewById(Resource.Id.resultLabel)
+ self.resultLabel.Text = str(self.count)
+
+ # Get our button from the layout resource,
+ # and attach an event to it
+ self.flipButton = self.main.FindViewById(Resource.Id.flipButton)
+ self.flipButton.Click += lambda sender, e: self.button_Click(sender, e)
+
+ def button_Click(self, sender, e):
+ self.count += 1
+ self.resultLabel.Text = str(self.count)
@@ -0,0 +1,47 @@
+using System;
+
+using Android.App;
+using Android.Content;
+using Android.Runtime;
+using Android.Views;
+using Android.Widget;
+using Android.OS;
+using Microsoft.Scripting.Hosting;
+using IronPython.Hosting;
+using System.IO;
+
+namespace AndroidSample {
+ [Activity(Label = "AndroidSample", MainLauncher = true, Icon = "@drawable/icon")]
+ public class FlippingGameActivity : Activity {
+ int count = 1;
+
+ ScriptEngine engine = Python.CreateEngine();
+ ScriptScope scope = null;
+ object shadow;
+
+ public FlippingGameActivity() {
+ this.scope = this.engine.CreateScope();
+ this.engine.Runtime.LoadAssembly(typeof(Resource).Assembly);
+ }
+
+ protected override void OnCreate(Bundle bundle) {
+ base.OnCreate(bundle);
+
+ InitShadow();
+ this.engine.Operations.InvokeMember(this.shadow, "OnCreate", bundle);
+ }
+
+ private void InitShadow() {
+ string code = null;
+ using(var shadowStream = new StreamReader(this.Assets.Open("FlippingGameActivity.py"))) {
+ code = shadowStream.ReadToEnd();
+ }
+
+ this.engine.Execute(code, this.scope);
+
+ object shadowClass = this.scope.GetVariable("FlippingGameActivity");
+ this.shadow = this.engine.Operations.CreateInstance(shadowClass, this);
+ }
+ }
+}
+
@@ -0,0 +1,41 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using Android.App;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("AndroidSample")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("AndroidSample")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("a557ce8c-9dbe-4b93-8fc4-95ffc126cf14")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
+
+// Add some common permissions, these can be removed if not needed
+[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
+[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
@@ -0,0 +1,44 @@
+Images, layout descriptions, binary blobs and string dictionaries can be included
+in your application as resource files. Various Android APIs are designed to
+operate on the resource IDs instead of dealing with images, strings or binary blobs
+directly.
+
+For example, a sample Android app that contains a user interface layout (Main.xml),
+an internationalization string table (Strings.xml) and some icons (drawable/Icon.png)
+would keep its resources in the "Resources" directory of the application:
+
+Resources/
+ Drawable/
+ Icon.png
+
+ Layout/
+ Main.axml
+
+ Values/
+ Strings.xml
+
+In order to get the build system to recognize Android resources, the build action should be set
+to "AndroidResource". The native Android APIs do not operate directly with filenames, but
+instead operate on resource IDs. When you compile an Android application that uses resources,
+the build system will package the resources for distribution and generate a class called
+"Resource" that contains the tokens for each one of the resources included. For example,
+for the above Resources layout, this is what the Resource class would expose:
+
+public class Resource {
+ public class Drawable {
+ public const int Icon = 0x123;
+ }
+
+ public class Layout {
+ public const int Main = 0x456;
+ }
+
+ public class String {
+ public const int FirstString = 0xabc;
+ public const int SecondString = 0xbcd;
+ }
+}
+
+You would then use Resource.Drawable.Icon to reference the Drawable/Icon.png file, or
+Resource.Layout.Main to reference the Layout/Main.axml file, or Resource.String.FirstString
+to reference the first string in the dictionary file Values/Strings.xml.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+ android:id="@+id/widget36"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ xmlns:android="http://schemas.android.com/apk/res/android">
+ <TableLayout
+ android:id="@+id/widget37"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical"
+ android:layout_alignParentTop="true"
+ android:layout_alignParentLeft="true">
+ <TableRow
+ android:id="@+id/widget42"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+ <TextView
+ android:id="@+id/widget45"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Bankroll" />
+ <TextView
+ android:id="@+id/bankrollLabel"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="\?" />
+ </TableRow>
+ <TableRow
+ android:id="@+id/widget43"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+ <TextView
+ android:id="@+id/widget46"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Wager" />
+ <EditText
+ android:id="@+id/wagerBox"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:numeric="integer" />
+ </TableRow>
+ <TableRow
+ android:id="@+id/widget44"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+ <TextView
+ android:id="@+id/widget47"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Guess" />
+ <RadioButton
+ android:id="@+id/guessHeads"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Heads"
+ android:checked="true" />
+ <RadioButton
+ android:id="@+id/guessTails"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Tails" />
+ </TableRow>
+ <TableRow
+ android:id="@+id/widget53"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+ <TextView
+ android:id="@+id/resultLabel"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="\?"
+ android:textSize="72sp"
+ android:textStyle="bold"
+ android:gravity="center"
+ android:layout_span="3" />
+ </TableRow>
+ <TableRow
+ android:id="@+id/widget55"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+ <Button
+ android:id="@+id/flipButton"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Flip!" />
+ <Button
+ android:id="@+id/resetButton"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Reset" />
+ </TableRow>
+ </TableLayout>
+</RelativeLayout>
Oops, something went wrong.

0 comments on commit 3d56e71

Please sign in to comment.