/
Resource.cs
184 lines (152 loc) · 5.95 KB
/
Resource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
using System.Xaml;
using Qube7.Composite.Design;
namespace Qube7.Composite.Data
{
/// <summary>
/// Implements a markup extension that binds property of the target object to the resource provided by the <see cref="ResourceProvider"/>.
/// </summary>
[MarkupExtensionReturnType(typeof(object))]
public class Resource : MarkupExtension
{
#region Fields
/// <summary>
/// The indexer format.
/// </summary>
private const string Indexer = "[{0}]";
/// <summary>
/// The name of the resource.
/// </summary>
private string name;
#endregion
#region Properties
/// <summary>
/// Gets or sets the source type used for retrieving the <see cref="ResourceProvider"/>.
/// </summary>
/// <value>The source type used for retrieving the <see cref="ResourceProvider"/>.</value>
public Type SourceType { get; set; }
/// <summary>
/// Gets or sets the name of the resource.
/// </summary>
/// <value>The name of the resource.</value>
[ConstructorArgument("name")]
public string Name
{
get { return name; }
set
{
Requires.NotNullOrEmpty(value, nameof(value));
name = value;
}
}
/// <summary>
/// Gets or sets a string that specifies how to format the resource if it displays the bound value as a string.
/// </summary>
/// <value>A string that specifies how to format the resource if it displays the bound value as a string.</value>
public string StringFormat { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Resource"/> class.
/// </summary>
/// <param name="name">The name of the resource.</param>
public Resource(string name)
{
Requires.NotNullOrEmpty(name, nameof(name));
this.name = name;
}
/// <summary>
/// Initializes a new instance of the <see cref="Resource"/> class.
/// </summary>
public Resource()
{
}
#endregion
#region Methods
/// <summary>
/// Returns an object that is provided as the value of the target property for this markup extension.
/// </summary>
/// <param name="serviceProvider">A service provider helper that can provide services for the markup extension.</param>
/// <returns>The object value to set on the property where the extension is applied.</returns>
/// <remarks>This method returns the <see cref="BindingExpression"/>, if the markup extension targets a <see cref="DependencyProperty"/> on a <see cref="DependencyObject"/>; otherwise, the <see cref="Binding"/> object with the resource binding source set.</remarks>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (name == null)
{
throw Error.InvalidOperation(Strings.ResourceNameExpected);
}
if (serviceProvider != null)
{
Type source = SourceType ?? GetRootType(serviceProvider);
if (source != null)
{
ResourceProvider provider = ResourceProvider.FromType(source);
Binding binding = new Binding(string.Format(Indexer, name)) { Source = provider, Mode = BindingMode.OneWay, StringFormat = StringFormat };
return binding.ProvideValue(serviceProvider);
}
}
return this;
}
/// <summary>
/// Gets the source type for the root object from markup or from an object graph.
/// </summary>
/// <param name="serviceProvider">The object that can provide services for the markup extension.</param>
/// <returns>The source type for the root object.</returns>
private static Type GetRootType(IServiceProvider serviceProvider)
{
IRootObjectProvider provider = serviceProvider.GetService<IRootObjectProvider>();
if (provider != null)
{
object root = provider.RootObject;
if (root != null)
{
if (Designer.DesignMode)
{
if (root is DependencyObject element)
{
return Designer.GetDesignType(element);
}
return null;
}
return root.GetType();
}
}
return null;
}
/// <summary>
/// Returns a <see cref="String"/> that represents the current <see cref="Resource"/>.
/// </summary>
/// <returns>A <see cref="String"/> that represents the current <see cref="Resource"/>.</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder();
builder.Append("{");
builder.Append(nameof(Resource));
builder.Append(" ");
builder.Append(name ?? string.Empty);
Type source = SourceType;
if (source != null)
{
builder.Append(", ");
builder.Append(nameof(SourceType));
builder.Append("=");
builder.Append(source.Name);
}
string format = StringFormat;
if (format != null)
{
builder.Append(", ");
builder.Append(nameof(StringFormat));
builder.Append("=");
builder.Append(format);
}
builder.Append("}");
return builder.ToString();
}
#endregion
}
}