Skip to content

Instantly share code, notes, and snippets.

@rohits79
rohits79 / gist:e4d76201958eaf02fe82035dc7510769
Created June 1, 2016 05:23
Caliburn static IOC functors
public static class IoC {
/// <summary>
/// Gets an instance by type and key.
/// </summary>
public static Func<Type, string, object> GetInstance = (service, key) => { throw new InvalidOperationException("IoC is not initialized."); };
/// <summary>
/// Gets all instances of a particular type.
/// </summary>
public static Func<Type, IEnumerable<object>> GetAllInstances = service => { throw new InvalidOperationException("IoC is not initialized."); };
public static class IoC {
/// <summary>
/// Gets an instance by type and key.
/// </summary>
public static Func<Type, string, object> GetInstance = (service, key) => { throw new InvalidOperationException("IoC is not initialized."); };
/// <summary>
/// Gets all instances of a particular type.
/// </summary>
public static Func<Type, IEnumerable<object>> GetAllInstances = service => { throw new InvalidOperationException("IoC is not initialized."); };
static Func<Type, Type> _defaultViewTypeToViewModelTypeResolver =
viewType =>
{
var viewName = viewType.FullName;
viewName = viewName.Replace(".Views.", ".ViewModels.");
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel";
var viewModelName = String.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName);
return Type.GetType(viewModelName);
};
static void Bind(object view, object viewModel)
{
FrameworkElement element = view as FrameworkElement;
if (element != null)
element.DataContext = viewModel;
}
--MainView.xaml
<UserControl x:Name="MainView"/>
--SubView.xaml
<UserControl x:Name="SubView"/>
--SubSubView.xaml
<UserControl x:Name="SubSubView"/>
<ContentControl DockPanel.Dock="Top" regions:RegionManager.RegionName="Toolbar"/>
<ContentControl regions:RegionManager.RegionName="Toolbar"/>
<ItemsControl regions:RegionManager.RegionName="Names"/>
<TabControl regions.RegionManager.RegionName="TabControl">
<DockPanel LastChildFill="True">
<ContentControl DockPanel.Dock="Top" regions:RegionManager.RegionName="Toolbar"></ContentControl>
<ItemsControl DockPanel.Dock="Left" regions:RegionManager.RegionName="Navigation"></ItemsControl>
<TabControl regions:RegionManager.RegionName="Tabs"></TabControl>
</DockPanel>
/// <summary>
/// Configures the default region adapter mappings to use in the application, in order
/// to adapt UI controls defined in XAML to use a region and register it automatically.
/// May be overwritten in a derived class to add specific mappings required by the application.
/// </summary>
/// <returns>The <see cref="RegionAdapterMappings"/> instance containing all the mappings.</returns>
protected virtual RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = ServiceLocator.Current.GetInstance<RegionAdapterMappings>();
if (regionAdapterMappings != null)
private void InnerAdd(object view, string viewName, IRegionManager scopedRegionManager)
{
if (this.ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null)
{
throw new InvalidOperationException(Resources.RegionViewExistsException);
}
...
}