Browse files

Added reflection extension demo

  • Loading branch information...
1 parent 71b8bfa commit 36c77a7de6f25a9172e99389958d45cf950ecf1a davidjade committed Nov 9, 2011
Showing with 302 additions and 117 deletions.
  1. +8 −3 MonoTouch.Dialog/Reflect.cs
  2. +178 −0 Sample/DemoReflectionExtensionApi.cs
  3. +2 −1 Sample/Main.cs
  4. +114 −113 Sample/Sample.csproj
View
11 MonoTouch.Dialog/Reflect.cs
@@ -450,13 +450,18 @@ public void Fetch ()
SetValue (mi, obj, fi.GetValue (null));
}
- } else SetValue (mi, obj, FetchElementValue (element));
+ } else {
+ object Value;
+ if(FetchElementValue(element, out Value))
+ SetValue (mi, obj, Value);
+ }
}
}
- public virtual object FetchElementValue (Element element)
+ public virtual bool FetchElementValue (Element element, out object Value)
{
- return null;
+ Value = null;
+ return false;
}
}
}
View
178 Sample/DemoReflectionExtensionApi.cs
@@ -0,0 +1,178 @@
+//
+// Sample showing the Extension Reflection-based API to create a dialog
+//
+using System;
+using System.Collections.Generic;
+using MonoTouch.Dialog;
+using MonoTouch.UIKit;
+using MonoTouch.Foundation;
+using System.Drawing;
+using MonoTouch.CoreGraphics;
+
+namespace Sample
+{
+ // Use the preserve attribute to inform the linker that even if I do not
+ // use the fields, to not try to optimize them away.
+
+ [Preserve (AllMembers=true)]
+ class ExtensionSettings {
+
+ [Section("Reflection Extension")]
+ [Caption("Custom Data Type")]
+ public CustomDataType custom;
+ }
+
+ public class CustomDataType {
+ public int value;
+
+ public override string ToString() {
+ return value.ToString();
+ }
+ }
+
+ class CustomElement : Element
+ {
+ static MonoTouch.Foundation.NSString key = new MonoTouch.Foundation.NSString("CustomElement");
+
+ CustomControl ui;
+
+ public CustomDataType Value
+ {
+ get { return new CustomDataType() { value = ui.Value }; }
+ set { ui.Value = value.value; }
+ }
+
+ public CustomElement(string Caption, CustomDataType Value)
+ : base(Caption)
+ {
+ ui = new CustomControl(new RectangleF(0, 0, 90, 20));
+ this.Value = Value;
+ }
+
+ protected override MonoTouch.Foundation.NSString CellKey
+ {
+ get {
+ return key;
+ }
+ }
+
+ public override UITableViewCell GetCell(UITableView tv)
+ {
+ var cell = tv.DequeueReusableCell(CellKey);
+ if (cell == null) {
+ cell = new UITableViewCell(UITableViewCellStyle.Default, CellKey);
+ cell.SelectionStyle = UITableViewCellSelectionStyle.None;
+ } else
+ RemoveTag(cell, 1);
+
+ cell.TextLabel.Text = Caption;
+ cell.AccessoryView = ui;
+
+ return cell;
+ }
+ }
+
+ class BindingContextEx : BindingContext
+ {
+ public BindingContextEx(object callbacks, object o, string title) : base(callbacks, o, title) { }
+
+ public override Element BuildElementForType(Type mType, string Caption, object Value)
+ {
+ if (mType == typeof(CustomDataType)) {
+ return new CustomElement(Caption, (CustomDataType)Value);
+ }
+
+ return base.BuildElementForType(mType, Caption, Value);
+ }
+
+ public override bool FetchElementValue(Element element, out object Value)
+ {
+ if (element.GetType() == typeof(CustomElement)) {
+ Value = ((CustomElement)element).Value;
+ return true;
+ }
+
+ return base.FetchElementValue(element, out Value);
+ }
+ }
+
+ class CustomControl : UIView
+ {
+ UIButton left, right;
+ UILabel text;
+
+ private int _Value;
+ public int Value
+ {
+ get { return _Value; }
+ set { _Value = value; text.Text = _Value.ToString(); }
+ }
+
+ public CustomControl(RectangleF frame)
+ : base(frame)
+ {
+ BackgroundColor = UIColor.Clear;
+
+ float controlwidth = frame.Width / 3f;
+ RectangleF controlframe = new RectangleF(frame.X, frame.Y, controlwidth, frame.Height);
+
+ left = UIButton.FromType(UIButtonType.RoundedRect);
+ left.Frame = controlframe;
+ left.SetTitle("+", UIControlState.Normal);
+ left.TouchUpInside += delegate {
+ Value++;
+ };
+
+ controlframe.Offset(controlwidth, 0);
+ text = new UILabel(controlframe) {
+ TextAlignment = UITextAlignment.Center,
+ Text = Value.ToString()
+ };
+ text.Layer.BorderColor = UIColor.Black.CGColor;
+ text.Layer.BorderWidth = 1f;
+
+ controlframe.Offset(controlwidth, 0);
+ right = UIButton.FromType(UIButtonType.RoundedRect);
+ right.Frame = controlframe;
+ right.SetTitle("-", UIControlState.Normal);
+ right.TouchUpInside += delegate {
+ Value--;
+ };
+
+ this.AddSubviews(left, text, right);
+ }
+ }
+
+ public partial class AppDelegate
+ {
+ ExtensionSettings ExtensionSettings;
+
+ public void DemoReflectionExtensionApi ()
+ {
+ if (ExtensionSettings == null) {
+ ExtensionSettings = new ExtensionSettings() {
+ custom = new CustomDataType() {
+ value = 1
+ }
+ };
+ }
+
+ var bc = new BindingContextEx(null, ExtensionSettings, "Settings");
+
+ var dv = new DialogViewController (bc.Root, true);
+
+ // When the view goes out of screen, we fetch the data.
+ dv.ViewDissapearing += delegate {
+ // This reflects the data back to the object instance
+ bc.Fetch ();
+
+ // Manly way of dumping the data.
+ Console.WriteLine ("Current status:");
+ Console.WriteLine (
+ "CustomDataType: {0}\n",
+ ExtensionSettings.custom.ToString());
+ };
+ navigation.PushViewController (dv, true);
+ }
+ }
+}
View
3 Sample/Main.cs
@@ -50,7 +50,8 @@ public override bool FinishedLaunching (UIApplication app, NSDictionary options)
new StringElement ("Index sample", DemoIndex),
},
new Section ("Auto-mapped", footer){
- new StringElement ("Reflection API", DemoReflectionApi)
+ new StringElement ("Reflection API", DemoReflectionApi),
+ new StringElement ("Reflection Extension API", DemoReflectionExtensionApi),
},
};
View
227 Sample/Sample.csproj
@@ -1,114 +1,115 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <PropertyGroup>
- <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
- <Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
- <ProductVersion>9.0.21022</ProductVersion>
- <SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>{F6AEFFC5-4807-49A3-83C5-661E655CB59A}</ProjectGuid>
- <ProjectTypeGuids>{6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
- <OutputType>Exe</OutputType>
- <RootNamespace>Sample</RootNamespace>
- <AssemblyName>Sample</AssemblyName>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
- <DebugSymbols>true</DebugSymbols>
- <DebugType>full</DebugType>
- <Optimize>false</Optimize>
- <OutputPath>bin\iPhoneSimulator\Debug</OutputPath>
- <DefineConstants>DEBUG</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- <MtouchLink>None</MtouchLink>
- <MtouchDebug>true</MtouchDebug>
- <MtouchExtraArgs>--nostrip</MtouchExtraArgs>
- <MtouchI18n />
- <MtouchUseArmv7>false</MtouchUseArmv7>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
- <DebugType>none</DebugType>
- <Optimize>false</Optimize>
- <OutputPath>bin\iPhoneSimulator\Release</OutputPath>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- <MtouchLink>Full</MtouchLink>
- <MtouchI18n />
- <MtouchSdkVersion>4.3</MtouchSdkVersion>
- <MtouchUseArmv7>false</MtouchUseArmv7>
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhone' ">
- <DebugSymbols>true</DebugSymbols>
- <DebugType>full</DebugType>
- <Optimize>false</Optimize>
- <OutputPath>bin\iPhone\Debug</OutputPath>
- <DefineConstants>DEBUG</DefineConstants>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- <MtouchDebug>true</MtouchDebug>
- <CodesignKey>iPhone Developer</CodesignKey>
- <MtouchLink>None</MtouchLink>
- <MtouchUseArmv7>false</MtouchUseArmv7>
- <IpaPackageName />
- <MtouchI18n />
- </PropertyGroup>
- <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
- <DebugType>none</DebugType>
- <Optimize>false</Optimize>
- <OutputPath>bin\iPhone\Release</OutputPath>
- <ErrorReport>prompt</ErrorReport>
- <WarningLevel>4</WarningLevel>
- <CodesignKey>iPhone Developer</CodesignKey>
- <MtouchLink>Full</MtouchLink>
- <MtouchI18n />
- <MtouchSdkVersion>3.0</MtouchSdkVersion>
- <MtouchUseArmv7>false</MtouchUseArmv7>
- </PropertyGroup>
- <ItemGroup>
- <Reference Include="System" />
- <Reference Include="System.Xml" />
- <Reference Include="System.Core" />
- <Reference Include="monotouch" />
- <Reference Include="System.Xml.Linq" />
- </ItemGroup>
- <ItemGroup>
- <Compile Include="MainWindow.xib.designer.cs">
- <DependentUpon>MainWindow.xib</DependentUpon>
- </Compile>
- <Compile Include="Main.cs" />
- <Compile Include="DemoElementApi.cs" />
- <Compile Include="DemoReflectionApi.cs" />
- <Compile Include="DemoDynamic.cs" />
- <Compile Include="DemoAddRemove.cs" />
- <Compile Include="DemoDate.cs" />
- <Compile Include="DemoHeadersFooters.cs" />
- <Compile Include="DemoLoadMore.cs" />
- <Compile Include="DemoEditing.cs" />
- <Compile Include="DemoOwnerDrawnElement.cs" />
- <Compile Include="DemoStyled.cs" />
- <Compile Include="DemoRefresh.cs" />
- <Compile Include="DemoContainerStyle.cs" />
- <Compile Include="DemoIndex.cs" />
- <Compile Include="DemoEditingAdvanced.cs" />
- </ItemGroup>
- <ItemGroup>
- <InterfaceDefinition Include="MainWindow.xib" xmlns="" />
- </ItemGroup>
- <ItemGroup>
- <ProjectReference Include="..\MonoTouch.Dialog\MonoTouch.Dialog.csproj">
- <Project>{3FFBFFF8-5560-4EDE-82E5-3FFDFBBA8A50}</Project>
- <Name>MonoTouch.Dialog</Name>
- </ProjectReference>
- </ItemGroup>
- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
- <ItemGroup>
- <Content Include="monodevelop-32.png" />
- <Content Include="caltemplate.png" />
- <Content Include="jakub-calendar.png" />
- <Content Include="favorite.png" />
- <Content Include="favorited.png" />
- <Content Include="background.png" />
- </ItemGroup>
- <ItemGroup>
- <None Include="Info.plist" />
- </ItemGroup>
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
+ <ProductVersion>9.0.21022</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{F6AEFFC5-4807-49A3-83C5-661E655CB59A}</ProjectGuid>
+ <ProjectTypeGuids>{6BC8ED88-2882-458C-8E55-DFD12B67127B};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>Sample</RootNamespace>
+ <AssemblyName>Sample</AssemblyName>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\iPhoneSimulator\Debug</OutputPath>
+ <DefineConstants>DEBUG</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <MtouchLink>None</MtouchLink>
+ <MtouchDebug>true</MtouchDebug>
+ <MtouchExtraArgs>--nostrip</MtouchExtraArgs>
+ <MtouchI18n />
+ <MtouchUseArmv7>false</MtouchUseArmv7>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
+ <DebugType>none</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\iPhoneSimulator\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <MtouchLink>Full</MtouchLink>
+ <MtouchI18n />
+ <MtouchSdkVersion>4.3</MtouchSdkVersion>
+ <MtouchUseArmv7>false</MtouchUseArmv7>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhone' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\iPhone\Debug</OutputPath>
+ <DefineConstants>DEBUG</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <MtouchDebug>true</MtouchDebug>
+ <CodesignKey>iPhone Developer</CodesignKey>
+ <MtouchLink>None</MtouchLink>
+ <MtouchUseArmv7>false</MtouchUseArmv7>
+ <IpaPackageName />
+ <MtouchI18n />
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhone' ">
+ <DebugType>none</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\iPhone\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <CodesignKey>iPhone Developer</CodesignKey>
+ <MtouchLink>Full</MtouchLink>
+ <MtouchI18n />
+ <MtouchSdkVersion>3.0</MtouchSdkVersion>
+ <MtouchUseArmv7>false</MtouchUseArmv7>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Xml" />
+ <Reference Include="System.Core" />
+ <Reference Include="monotouch" />
+ <Reference Include="System.Xml.Linq" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="MainWindow.xib.designer.cs">
+ <DependentUpon>MainWindow.xib</DependentUpon>
+ </Compile>
+ <Compile Include="Main.cs" />
+ <Compile Include="DemoElementApi.cs" />
+ <Compile Include="DemoReflectionApi.cs" />
+ <Compile Include="DemoDynamic.cs" />
+ <Compile Include="DemoAddRemove.cs" />
+ <Compile Include="DemoDate.cs" />
+ <Compile Include="DemoHeadersFooters.cs" />
+ <Compile Include="DemoLoadMore.cs" />
+ <Compile Include="DemoEditing.cs" />
+ <Compile Include="DemoOwnerDrawnElement.cs" />
+ <Compile Include="DemoStyled.cs" />
+ <Compile Include="DemoRefresh.cs" />
+ <Compile Include="DemoContainerStyle.cs" />
+ <Compile Include="DemoIndex.cs" />
+ <Compile Include="DemoEditingAdvanced.cs" />
+ <Compile Include="DemoReflectionExtensionApi.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <InterfaceDefinition Include="MainWindow.xib" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\MonoTouch.Dialog\MonoTouch.Dialog.csproj">
+ <Project>{3FFBFFF8-5560-4EDE-82E5-3FFDFBBA8A50}</Project>
+ <Name>MonoTouch.Dialog</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ItemGroup>
+ <Content Include="monodevelop-32.png" />
+ <Content Include="caltemplate.png" />
+ <Content Include="jakub-calendar.png" />
+ <Content Include="favorite.png" />
+ <Content Include="favorited.png" />
+ <Content Include="background.png" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="Info.plist" />
+ </ItemGroup>
</Project>

0 comments on commit 36c77a7

Please sign in to comment.