XPathMessageContextMarkupExtension.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.XamlIntegration
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ServiceModel.Dispatcher;
  9. using System.Windows.Markup;
  10. [ContentProperty("Namespaces")]
  11. public class XPathMessageContextMarkupExtension : MarkupExtension
  12. {
  13. static List<string> implicitPrefixes;
  14. Dictionary<string, string> namespaces;
  15. static XPathMessageContextMarkupExtension()
  16. {
  17. implicitPrefixes = new List<string>();
  18. foreach (string prefix in XPathMessageContext.defaultNamespaces.Keys)
  19. {
  20. implicitPrefixes.Add(prefix);
  21. }
  22. implicitPrefixes.Add("");
  23. implicitPrefixes.Add("xml");
  24. implicitPrefixes.Add("xmlns");
  25. }
  26. public XPathMessageContextMarkupExtension()
  27. {
  28. this.namespaces = new Dictionary<string, string>();
  29. }
  30. public XPathMessageContextMarkupExtension(XPathMessageContext context)
  31. : this()
  32. {
  33. foreach (string prefix in context)
  34. {
  35. if (!implicitPrefixes.Contains(prefix))
  36. {
  37. this.namespaces.Add(prefix, context.LookupNamespace(prefix));
  38. }
  39. }
  40. }
  41. public Dictionary<string, string> Namespaces
  42. {
  43. get { return this.namespaces; }
  44. }
  45. public override object ProvideValue(IServiceProvider serviceProvider)
  46. {
  47. XPathMessageContext context = new XPathMessageContext();
  48. foreach (KeyValuePair<string, string> ns in this.namespaces)
  49. {
  50. context.AddNamespace(ns.Key, ns.Value);
  51. }
  52. return context;
  53. }
  54. }
  55. }