Skip to content

Commit

Permalink
#371 Add iOS to Caliburn.Micro.Platform
Browse files Browse the repository at this point in the history
  • Loading branch information
nigel-sampson committed Jun 5, 2017
1 parent f73b606 commit 0903272
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 19 deletions.
24 changes: 23 additions & 1 deletion src/Caliburn.Micro.Platform/Caliburn.Micro.Platform.csproj
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;uap10.0;MonoAndroid10</TargetFrameworks>
<TargetFrameworks>net45;uap10.0;MonoAndroid10;Xamarin.iOS</TargetFrameworks>
<AssemblyName>Caliburn.Micro.Platform</AssemblyName>
<RootNamespace>Caliburn.Micro</RootNamespace>
</PropertyGroup>
Expand All @@ -25,6 +25,13 @@
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'Xamarin.iOS'">
<DefineConstants>IOS</DefineConstants>
<TargetFrameworkIdentifier>Xamarin.iOS</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Platforms\**\*.*" />
<None Include="Platforms\**\*.*" />
Expand Down Expand Up @@ -65,6 +72,21 @@
<Reference Include="System.Xml" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'Xamarin.iOS'">
<Compile Remove="*.cs" />
<Compile Include="Platforms\iOS\*.cs" />
<Reference Include="System" />
<Reference Include="System.Runtime" />
<Reference Include="System.Collections" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="Xamarin.iOS" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MSBuild.Sdk.Extras" Version="1.0.2" />
</ItemGroup>
Expand Down
@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Foundation;
using UIKit;

namespace Caliburn.Micro
{
/// <summary>
/// Encapsulates the app and its available services.
/// </summary>
public class CaliburnApplicationDelegate : UIApplicationDelegate
{
private bool isInitialized;

/// <summary>
/// Creates an instance of <see cref="CaliburnApplicationDelegate"/>.
/// </summary>
public CaliburnApplicationDelegate() {

}

/// <summary>
/// Creates an instance of <see cref="CaliburnApplicationDelegate"/>.
/// </summary>
/// <param name="handle">/// The handle for this class</param>
public CaliburnApplicationDelegate(IntPtr handle) : base(handle) {

}

/// <summary>
/// Creates an instance of <see cref="CaliburnApplicationDelegate"/>.
/// </summary>
/// <param name="t">>Unused sentinel value, pass NSObjectFlag.Empty</param>
public CaliburnApplicationDelegate(NSObjectFlag t)
: base(t)
{

}

/// <summary>
/// Called by the bootstrapper's constructor at design time to start the framework.
/// </summary>
protected virtual void StartDesignTime()
{
AssemblySource.Instance.Clear();
AssemblySource.Instance.AddRange(SelectAssemblies());

Configure();

IoC.GetInstance = GetInstance;
IoC.GetAllInstances = GetAllInstances;
IoC.BuildUp = BuildUp;
}

/// <summary>
/// Called by the bootstrapper's constructor at runtime to start the framework.
/// </summary>B
protected virtual void StartRuntime()
{
EventAggregator.HandlerResultProcessing = (target, result) =>
{
var task = result as Task;
if (task != null)
{
result = new IResult[] { task.AsResult() };
}
var coroutine = result as IEnumerable<IResult>;
if (coroutine != null)
{
var viewAware = target as IViewAware;
var view = viewAware != null ? viewAware.GetView() : null;
var context = new CoroutineExecutionContext { Target = target, View = view };
Coroutine.BeginExecute(coroutine.GetEnumerator(), context);
}
};

AssemblySourceCache.Install();
AssemblySource.Instance.AddRange(SelectAssemblies());

Configure();
IoC.GetInstance = GetInstance;
IoC.GetAllInstances = GetAllInstances;
IoC.BuildUp = BuildUp;
}

/// <summary>
/// Start the framework.
/// </summary>
protected void Initialize()
{
if (isInitialized)
{
return;
}

isInitialized = true;

PlatformProvider.Current = new IOSPlatformProvider();

if (Execute.InDesignMode)
{
try
{
StartDesignTime();
}
catch
{
//if something fails at design-time, there's really nothing we can do...
isInitialized = false;
throw;
}
}
else
{
StartRuntime();
}
}

/// <summary>
/// Override to configure the framework and setup your IoC container.
/// </summary>
protected virtual void Configure()
{
}

/// <summary>
/// Override to tell the framework where to find assemblies to inspect for views, etc.
/// </summary>
/// <returns>A list of assemblies to inspect.</returns>
protected virtual IEnumerable<Assembly> SelectAssemblies()
{
return new[] { GetType().GetTypeInfo().Assembly };
}

/// <summary>
/// Override this to provide an IoC specific implementation.
/// </summary>
/// <param name="service">The service to locate.</param>
/// <param name="key">The key to locate.</param>
/// <returns>The located service.</returns>
protected virtual object GetInstance(Type service, string key)
{
return Activator.CreateInstance(service);
}

/// <summary>
/// Override this to provide an IoC specific implementation
/// </summary>
/// <param name="service">The service to locate.</param>
/// <returns>The located services.</returns>
protected virtual IEnumerable<object> GetAllInstances(Type service)
{
return new[] { Activator.CreateInstance(service) };
}

/// <summary>
/// Override this to provide an IoC specific implementation.
/// </summary>
/// <param name="instance">The instance to perform injection on.</param>
protected virtual void BuildUp(object instance)
{
}
}
}
25 changes: 25 additions & 0 deletions src/Caliburn.Micro.Platform/Platforms/iOS/IUIViewController.cs
@@ -0,0 +1,25 @@
using System;

namespace Caliburn.Micro
{
/// <summary>
/// An interface to allow the IOSPlatformProvider provide view lifecycle events
/// </summary>
public interface IUIViewController
{
/// <summary>
/// Returns if the current view is already loaded
/// </summary>
bool IsViewLoaded { get; }

/// <summary>
/// Invoked when the view is loaded
/// </summary>
event EventHandler ViewLoaded;

/// <summary>
/// Invoked the view appears
/// </summary>
event EventHandler ViewAppeared;
}
}

0 comments on commit 0903272

Please sign in to comment.