Skip to content

Commit

Permalink
DataTemplate selector with caliburn.micro
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoskinen committed Jan 20, 2012
1 parent 045daaa commit a4f324d
Show file tree
Hide file tree
Showing 96 changed files with 14,792 additions and 0 deletions.
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "caliburn-micro-datatemplate-selector", "caliburn-micro-datatemplate-selector\caliburn-micro-datatemplate-selector.csproj", "{9A26B2AB-0A73-4148-A814-2A3965956C90}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9A26B2AB-0A73-4148-A814-2A3965956C90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A26B2AB-0A73-4148-A814-2A3965956C90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A26B2AB-0A73-4148-A814-2A3965956C90}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{9A26B2AB-0A73-4148-A814-2A3965956C90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A26B2AB-0A73-4148-A814-2A3965956C90}.Release|Any CPU.Build.0 = Release|Any CPU
{9A26B2AB-0A73-4148-A814-2A3965956C90}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
@@ -0,0 +1,11 @@
<Application
x:Class="caliburn_micro_datatemplate_selector.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:caliburn_micro_datatemplate_selector">

<Application.Resources>
<local:MyBootstrapper x:Key="bootstrapper"/>
</Application.Resources>

</Application>
@@ -0,0 +1,12 @@
using System.Windows;

namespace caliburn_micro_datatemplate_selector
{
public partial class App : Application
{
public App()
{
InitializeComponent();
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,32 @@
using Caliburn.Micro;

namespace caliburn_micro_datatemplate_selector
{
public class MyBootstrapper : Bootstrapper
{
private SimpleContainer container;
protected override void Configure()
{
container = new SimpleContainer();

container.PerRequest<MainPageViewModel>();

Caliburn.Micro.ViewLocator.NameTransformer.AddRule("");
}

protected override object GetInstance(System.Type service, string key)
{
return container.GetInstance(service, key);
}

protected override System.Collections.Generic.IEnumerable<object> GetAllInstances(System.Type service)
{
return container.GetAllInstances(service);
}

protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
}
}
@@ -0,0 +1,108 @@
namespace Caliburn.Micro {
using System;
using System.Linq;
using System.Reflection;

/// <summary>
/// Extension methods for the <see cref="SimpleContainer"/>.
/// </summary>
public static class ContainerExtensions {
/// <summary>
/// Registers a singleton.
/// </summary>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="container">The container.</param>
/// <returns>The container.</returns>
public static SimpleContainer Singleton<TImplementation>(this SimpleContainer container) {
container.RegisterSingleton(typeof(TImplementation), null, typeof(TImplementation));
return container;
}

/// <summary>
/// Registers a singleton.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="container">The container.</param>
/// <returns>The container.</returns>
public static SimpleContainer Singleton<TService, TImplementation>(this SimpleContainer container)
where TImplementation : TService {
container.RegisterSingleton(typeof(TService), null, typeof(TImplementation));
return container;
}

/// <summary>
/// Registers an service to be created on each request.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="container">The container.</param>
/// <returns>The container.</returns>
public static SimpleContainer PerRequest<TService, TImplementation>(this SimpleContainer container)
where TImplementation : TService {
container.RegisterPerRequest(typeof(TService), null, typeof(TImplementation));
return container;
}

/// <summary>
/// Registers an service to be created on each request.
/// </summary>
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
/// <param name="container">The container.</param>
/// <returns>The container.</returns>
public static SimpleContainer PerRequest<TImplementation>(this SimpleContainer container) {
container.RegisterPerRequest(typeof(TImplementation), null, typeof(TImplementation));
return container;
}

/// <summary>
/// Registers an instance with the container.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="container">The container.</param>
/// <param name="instance">The instance.</param>
/// <returns>The container.</returns>
public static SimpleContainer Instance<TService>(this SimpleContainer container, TService instance) {
container.RegisterInstance(typeof(TService), null, instance);
return container;
}

/// <summary>
/// Registers a custom service handler with the container.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="container">The container.</param>
/// <param name="handler">The handler.</param>
/// <returns>The container.</returns>
public static SimpleContainer Handler<TService>(this SimpleContainer container, Func<SimpleContainer, object> handler) {
container.RegisterHandler(typeof(TService), null, handler);
return container;
}

/// <summary>
/// Registers all specified types in an assembly as singletong in the container.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <param name="container">The container.</param>
/// <param name="assembly">The assembly.</param>
/// <param name="filter">The type filter.</param>
/// <returns>The container.</returns>
public static SimpleContainer AllTypesOf<TService>(this SimpleContainer container, Assembly assembly, Func<Type, bool> filter = null) {
if(filter == null)
filter = type => true;

var types = from type in assembly.GetTypes()
where typeof(TService).IsAssignableFrom(type)
&& !type.IsAbstract
&& !type.IsInterface
&& filter(type)
select type;

foreach(var type in types) {
container.RegisterSingleton(typeof(TService), null, type);
}

return container;
}
}
}
@@ -0,0 +1,87 @@
namespace Caliburn.Micro
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

/// <summary>
/// Generic extension methods used by the framework.
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// Gets all the attributes of a particular type.
/// </summary>
/// <typeparam name="T">The type of attributes to get.</typeparam>
/// <param name="member">The member to inspect for attributes.</param>
/// <param name="inherit">Whether or not to search for inherited attributes.</param>
/// <returns>The list of attributes found.</returns>
public static IEnumerable<T> GetAttributes<T>(this MemberInfo member, bool inherit) {
return Attribute.GetCustomAttributes(member, inherit).OfType<T>();
}

/// <summary>
/// Applies the action to each element in the list.
/// </summary>
/// <typeparam name="T">The enumerable item's type.</typeparam>
/// <param name="enumerable">The elements to enumerate.</param>
/// <param name="action">The action to apply to each item in the list.</param>
public static void Apply<T>(this IEnumerable<T> enumerable, Action<T> action) {
foreach(var item in enumerable)
action(item);
}

/// <summary>
/// Converts an expression into a <see cref="MemberInfo"/>.
/// </summary>
/// <param name="expression">The expression to convert.</param>
/// <returns>The member info.</returns>
public static MemberInfo GetMemberInfo(this Expression expression) {
var lambda = (LambdaExpression)expression;

MemberExpression memberExpression;
if(lambda.Body is UnaryExpression) {
var unaryExpression = (UnaryExpression)lambda.Body;
memberExpression = (MemberExpression)unaryExpression.Operand;
}
else
memberExpression = (MemberExpression)lambda.Body;

return memberExpression.Member;
}

#if WP7
//Method missing in WP7 Linq

/// <summary>
/// Merges two sequences by using the specified predicate function.
/// </summary>
/// <typeparam name="TFirst">The type of the elements of the first input sequence.</typeparam>
/// <typeparam name="TSecond">The type of the elements of the second input sequence.</typeparam>
/// <typeparam name="TResult">The type of the elements of the result sequence.</typeparam>
/// <param name="first">The first sequence to merge.</param>
/// <param name="second">The second sequence to merge.</param>
/// <param name="resultSelector"> A function that specifies how to merge the elements from the two sequences.</param>
/// <returns>An System.Collections.Generic.IEnumerable&lt;T&gt; that contains merged elements of two input sequences.</returns>
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
{
if (first == null)
throw new ArgumentNullException("first");
if (second == null)
throw new ArgumentNullException("second");
if (resultSelector == null)
throw new ArgumentNullException("resultSelector");

var enumFirst = first.GetEnumerator();
var enumSecond = second.GetEnumerator();

while (enumFirst.MoveNext() && enumSecond.MoveNext()) {
yield return resultSelector(enumFirst.Current, enumSecond.Current);
}
}

#endif
}
}

0 comments on commit a4f324d

Please sign in to comment.