Browse Source

XAML Designer Demo Project -> Add the Posibility to Add Controls from other Assemblys to the ToolBox

(needed for further testing)
pull/52/head
jkuehner 13 years ago
parent
commit
3a2ec306e5
  1. 101
      samples/XamlDesigner/Toolbox.cs
  2. 13
      samples/XamlDesigner/ToolboxView.xaml
  3. 17
      samples/XamlDesigner/ToolboxView.xaml.cs

101
samples/XamlDesigner/Toolbox.cs

@ -14,45 +14,45 @@ namespace ICSharpCode.XamlDesigner
public class Toolbox public class Toolbox
{ {
public Toolbox() public Toolbox()
{ {
AssemblyNodes = new ObservableCollection<AssemblyNode>(); AssemblyNodes = new ObservableCollection<AssemblyNode>();
LoadSettings(); LoadSettings();
} }
public static Toolbox Instance = new Toolbox(); public static Toolbox Instance = new Toolbox();
public ObservableCollection<AssemblyNode> AssemblyNodes { get; private set; } public ObservableCollection<AssemblyNode> AssemblyNodes { get; private set; }
public void AddAssembly(string path) public void AddAssembly(string path)
{ {
AddAssembly(path, true); AddAssembly(path, true);
} }
void AddAssembly(string path, bool updateSettings) void AddAssembly(string path, bool updateSettings)
{ {
var assembly = Assembly.LoadFile(path); var assembly = Assembly.LoadFile(path);
var node = new AssemblyNode(); var node = new AssemblyNode();
node.Assembly = assembly; node.Assembly = assembly;
node.Path = path; node.Path = path;
foreach (var t in assembly.GetExportedTypes()) { foreach (var t in assembly.GetExportedTypes()) {
if (IsControl(t) && Metadata.IsPopularControl(t)) { if (IsControl(t) /* && Metadata.IsPopularControl(t) */) {
node.Controls.Add(new ControlNode() { Type = t }); node.Controls.Add(new ControlNode() { Type = t });
} }
} }
node.Controls.Sort(delegate(ControlNode c1, ControlNode c2) { node.Controls.Sort(delegate(ControlNode c1, ControlNode c2) {
return c1.Name.CompareTo(c2.Name); return c1.Name.CompareTo(c2.Name);
}); });
AssemblyNodes.Add(node); AssemblyNodes.Add(node);
if (updateSettings) { if (updateSettings) {
if (Settings.Default.AssemblyList == null) { if (Settings.Default.AssemblyList == null) {
Settings.Default.AssemblyList = new StringCollection(); Settings.Default.AssemblyList = new StringCollection();
} }
Settings.Default.AssemblyList.Add(path); Settings.Default.AssemblyList.Add(path);
} }
} }
public void Remove(AssemblyNode node) public void Remove(AssemblyNode node)
{ {
@ -61,42 +61,47 @@ namespace ICSharpCode.XamlDesigner
} }
public void LoadSettings() public void LoadSettings()
{ {
if (Settings.Default.AssemblyList != null) { if (Settings.Default.AssemblyList != null) {
foreach (var path in Settings.Default.AssemblyList) { foreach (var path in Settings.Default.AssemblyList) {
AddAssembly(Environment.ExpandEnvironmentVariables(path), false); try
{
AddAssembly(Environment.ExpandEnvironmentVariables(path), false);
}
catch (Exception ex)
{ }
} }
} }
} }
static bool IsControl(Type t) static bool IsControl(Type t)
{ {
return !t.IsAbstract && !t.IsGenericTypeDefinition && t.IsSubclassOf(typeof(FrameworkElement)); return !t.IsAbstract && !t.IsGenericTypeDefinition && t.IsSubclassOf(typeof(FrameworkElement));
} }
} }
public class AssemblyNode public class AssemblyNode
{ {
public AssemblyNode() public AssemblyNode()
{ {
Controls = new List<ControlNode>(); Controls = new List<ControlNode>();
} }
public Assembly Assembly { get; set; } public Assembly Assembly { get; set; }
public List<ControlNode> Controls { get; private set; } public List<ControlNode> Controls { get; private set; }
public string Path { get; set; } public string Path { get; set; }
public string Name { public string Name {
get { return Assembly.GetName().Name; } get { return Assembly.GetName().Name; }
} }
} }
public class ControlNode public class ControlNode
{ {
public Type Type { get; set; } public Type Type { get; set; }
public string Name { public string Name {
get { return Type.Name; } get { return Type.Name; }
} }
} }
} }

13
samples/XamlDesigner/ToolboxView.xaml

@ -3,18 +3,23 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Outline="clr-namespace:ICSharpCode.WpfDesign.Designer.OutlineView;assembly=ICSharpCode.WpfDesign.Designer" xmlns:Outline="clr-namespace:ICSharpCode.WpfDesign.Designer.OutlineView;assembly=ICSharpCode.WpfDesign.Designer"
xmlns:Default="clr-namespace:ICSharpCode.XamlDesigner"> xmlns:Default="clr-namespace:ICSharpCode.XamlDesigner">
<UserControl.ContextMenu>
<ContextMenu>
<MenuItem Header="Browse..." Click="BrowseForAssemblies_OnClick"></MenuItem>
</ContextMenu>
</UserControl.ContextMenu>
<UserControl.Resources> <UserControl.Resources>
<HierarchicalDataTemplate DataType="{x:Type Default:AssemblyNode}" <HierarchicalDataTemplate DataType="{x:Type Default:AssemblyNode}"
ItemsSource="{Binding Controls}"> ItemsSource="{Binding Controls}">
<Outline:IconItem Icon="Images/Reference.png" <Outline:IconItem Icon="Images/Reference.png"
Text="{Binding Name}" Text="{Binding Name}"
ToolTip="{Binding Path}" /> ToolTip="{Binding Path}" />
</HierarchicalDataTemplate> </HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type Default:ControlNode}"> <DataTemplate DataType="{x:Type Default:ControlNode}">
<Outline:IconItem Icon="Images/Tag.png" <Outline:IconItem Icon="Images/Tag.png"
Text="{Binding Type.Name}" /> Text="{Binding Type.Name}" />
</DataTemplate> </DataTemplate>
</UserControl.Resources> </UserControl.Resources>

17
samples/XamlDesigner/ToolboxView.xaml.cs

@ -1,3 +1,4 @@
using System.IO;
using ICSharpCode.WpfDesign.Designer.OutlineView; using ICSharpCode.WpfDesign.Designer.OutlineView;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,6 +17,7 @@ using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using ICSharpCode.WpfDesign.Designer.Services; using ICSharpCode.WpfDesign.Designer.Services;
using Microsoft.Win32;
namespace ICSharpCode.XamlDesigner namespace ICSharpCode.XamlDesigner
{ {
@ -73,5 +75,20 @@ namespace ICSharpCode.XamlDesigner
Toolbox.Instance.Remove(node); Toolbox.Instance.Remove(node);
} }
} }
private void BrowseForAssemblies_OnClick(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Filter = "Assemblies (*.dll)|*.dll";
dlg.Multiselect = true;
dlg.CheckFileExists = true;
if (dlg.ShowDialog().Value)
{
foreach (var fileName in dlg.FileNames)
{
Toolbox.Instance.AddAssembly(fileName);
}
}
}
} }
} }

Loading…
Cancel
Save