//
//
//
//
// $Revision$
//
using System;
using System.Xml;
using ICSharpCode.WpfDesign.XamlDom;
using ICSharpCode.WpfDesign.Designer.Services;
using ICSharpCode.WpfDesign.Extensions;
namespace ICSharpCode.WpfDesign.Designer.Xaml
{
///
/// The design context implementation used when editing XAML.
///
public sealed class XamlDesignContext : DesignContext
{
readonly XamlDocument _doc;
readonly XamlDesignItem _rootItem;
internal readonly XamlComponentService _componentService;
///
/// Creates a new XamlDesignContext instance.
///
public XamlDesignContext(XmlReader xamlReader)
{
if (xamlReader == null)
throw new ArgumentNullException("xamlReader");
this.Services.AddService(typeof(IVisualDesignService), new DefaultVisualDesignService());
this.Services.AddService(typeof(ISelectionService), new DefaultSelectionService());
this.Services.AddService(typeof(IToolService), new DefaultToolService());
this.Services.AddService(typeof(UndoService), new UndoService());
_componentService = new XamlComponentService(this);
this.Services.AddService(typeof(IComponentService), _componentService);
// register extensions from this assembly:
this.Services.ExtensionManager.RegisterAssembly(typeof(XamlDesignContext).Assembly);
XamlParserSettings xamlParseSettings = new XamlParserSettings();
xamlParseSettings.CreateInstanceCallback = OnXamlParserCreateInstance;
_doc = XamlParser.Parse(xamlReader, xamlParseSettings);
_rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement);
}
object OnXamlParserCreateInstance(Type instanceType, object[] arguments)
{
foreach (Type extensionType in this.Services.ExtensionManager.GetExtensionTypes(instanceType)) {
if (typeof(CustomInstanceFactory).IsAssignableFrom(extensionType)) {
CustomInstanceFactory factory = (CustomInstanceFactory)Activator.CreateInstance(extensionType);
return factory.CreateInstance(instanceType, arguments);
}
}
return CustomInstanceFactory.DefaultInstanceFactory.CreateInstance(instanceType, arguments);
}
///
/// Saves the XAML DOM into the XML writer.
///
public override void Save(System.Xml.XmlWriter writer)
{
_doc.Save(writer);
}
///
/// Gets the root item being designed.
///
public override DesignItem RootItem {
get { return _rootItem; }
}
}
}