Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ReferenceMarkupExtension.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Windows.Markup;
5 using System.Xaml;
6 
7 namespace SiliconStudio.Presentation.MarkupExtensions
8 {
9  [ContentProperty("Name")]
10  [Obsolete]
11  public class Reference : MarkupExtension
12  {
13  public Reference()
14  {
15  }
16 
17  public Reference(string name)
18  {
19  Name = name;
20  }
21 
22  [ConstructorArgument("name")]
23  public string Name { get; set; }
24 
25  public override object ProvideValue(IServiceProvider serviceProvider)
26  {
27  if (serviceProvider == null)
28  throw new ArgumentNullException("serviceProvider");
29 
30  // the bellow method do not work when no Application has been created
31  /*
32  object[] references = (from window in Application.Current.Windows.Cast<Window>()
33  let name = window.FindName(Name)
34  where name != null
35  select name)
36  .ToArray();
37 
38  if (references.Length == 0)
39  throw new InvalidOperationException(string.Format("Impossible to find reference for name '{0}'", Name));
40 
41  if (references.Length > 1)
42  {
43  StringBuilder message = new StringBuilder();
44  message.AppendLine(string.Format("There are {0} references for name '{1}'.", references.Length, Name));
45  foreach (object reference in references)
46  message.AppendLine(string.Format(" reference: '{0}' ({1})", reference, reference.GetType().FullName));
47  throw new InvalidOperationException(message.ToString());
48  }
49 
50  return references[0];
51  */
52 
53  IXamlNameResolver service = serviceProvider.GetService(typeof(IXamlNameResolver)) as IXamlNameResolver;
54  if (service == null)
55  return null; // happens in design mode
56 
57  if (string.IsNullOrWhiteSpace(Name))
58  throw new InvalidOperationException("Reference markup extension must be provided a name.");
59 
60  object fixupToken = service.Resolve(Name);
61 
62  if (fixupToken == null)
63  fixupToken = service.GetFixupToken(new [] { Name }, true);
64 
65  return fixupToken;
66  }
67  }
68 }
override object ProvideValue(IServiceProvider serviceProvider)