2 namespace System.Activities.Presentation.Model {
4 using System.Activities.Presentation.Services;
5 using System.Activities.Presentation;
10 /// The ModelFactory class should be used to create instances
11 /// of items in the designer. ModelFactory is designed to be
12 /// a static API for convenience. The underlying implementation
13 /// of this API simply calls through to the ModelService
\92s
14 /// CreateItem method.
16 public static class ModelFactory {
19 /// Creates a new item for the given item type.
21 /// <param name="context">
22 /// The designer's editing context.
24 /// <param name="itemType">
25 /// The type of item to create.
27 /// <param name="arguments">
28 /// An optional array of arguments that should be passed to the constructor of the item.
31 /// The newly created item type.
33 /// <exception cref="ArgumentNullException">if itemType or context is null.</exception>
34 /// <exception cref="InvalidOperationException">if there is no editing model in the context that can create new items.</exception>
35 public static ModelItem CreateItem(EditingContext context, Type itemType, params object[] arguments) {
36 return CreateItem(context, itemType, CreateOptions.None, arguments);
40 /// Creates a new item for the given item type.
42 /// <param name="context">
43 /// The designer's editing context.
45 /// <param name="itemType">
46 /// The type of item to create.
48 /// <param name="options">
49 /// A set of create options to use when creating the item. The default value is CreateOptions.None.
51 /// <param name="arguments">
52 /// An optional array of arguments that should be passed to the constructor of the item.
55 /// The newly created item type.
57 /// <exception cref="ArgumentNullException">if itemType or context is null.</exception>
58 /// <exception cref="InvalidOperationException">if there is no editing model in the context that can create new items.</exception>
59 public static ModelItem CreateItem(EditingContext context, Type itemType, CreateOptions options, params object[] arguments) {
60 if (context == null) throw FxTrace.Exception.ArgumentNull("context");
61 if (itemType == null) throw FxTrace.Exception.ArgumentNull("itemType");
62 if (!EnumValidator.IsValid(options)) throw FxTrace.Exception.AsError(new ArgumentOutOfRangeException("options"));
64 ModelService ms = context.Services.GetRequiredService<ModelService>();
65 return ms.InvokeCreateItem(itemType, options, arguments);
69 /// Creates a new model item by creating a deep copy of the isntance provided.
71 /// <param name="context">
72 /// The designer's editing context.
74 /// <param name="item">
75 /// The item to clone.
78 /// The newly created item.
80 public static ModelItem CreateItem(EditingContext context, object item) {
81 if (context == null) throw FxTrace.Exception.ArgumentNull("context");
82 if (item == null) throw FxTrace.Exception.ArgumentNull("item");
84 ModelService ms = context.Services.GetRequiredService<ModelService>();
85 return ms.InvokeCreateItem(item);
89 /// Create a new model item that represents a the value of a static member of a the given class.
90 /// For example, to add a reference to Brushes.Red to the model call this methods with
91 /// typeof(Brushes) and the string "Red". This will be serialized into XAML as
92 /// {x:Static Brushes.Red}.
94 /// <param name="context">
95 /// The designer's editing context.
97 /// <param name="type">
98 /// The type that contains the static member being referenced.
100 /// <param name="memberName">
101 /// The name of the static member being referenced.
103 /// <returns></returns>
104 public static ModelItem CreateStaticMemberItem(EditingContext context, Type type, string memberName) {
105 if (context == null) throw FxTrace.Exception.ArgumentNull("context");
106 if (type == null) throw FxTrace.Exception.ArgumentNull("type");
107 if (memberName == null) throw FxTrace.Exception.ArgumentNull("memberName");
109 ModelService ms = context.Services.GetRequiredService<ModelService>();
110 return ms.InvokeCreateStaticMemberItem(type, memberName);