Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Xaml;
  5. using System.Xaml.Schema;
  6.  
  7. namespace Sandbox
  8. {
  9.  
  10.     public class Foo
  11.     {
  12.         public object DataContext { get; set; }
  13.     }
  14.    
  15.     class Program
  16.     {
  17.         private static XamlDirective DesignContextDirective = new XamlDirective(
  18.             new[] { "http://schemas.microsoft.com/expression/blend/2008" },
  19.             "DataContext", XamlLanguage.Object, null, AllowedMemberLocations.Attribute);
  20.  
  21.         class CustomContext : XamlSchemaContext
  22.         {
  23.             public bool IsDesignMode { get; set; }
  24.             public override XamlDirective GetXamlDirective(string xamlNamespace, string name)
  25.             {
  26.                
  27.                 if (IsDesignMode && name == DesignContextDirective.Name && xamlNamespace == DesignContextDirective.PreferredXamlNamespace)
  28.                 {
  29.                     return DesignContextDirective;
  30.                 }
  31.                 return base.GetXamlDirective(xamlNamespace, name);
  32.             }
  33.  
  34.             public override bool TryGetCompatibleXamlNamespace(string xamlNamespace, out string compatibleNamespace)
  35.             {
  36.                 //Forces XamlXmlReader to not ignore our namespace if mc:Ignorable is set
  37.                 if (
  38.                     IsDesignMode &&
  39.                     xamlNamespace == DesignContextDirective.PreferredXamlNamespace)
  40.                 {
  41.                     compatibleNamespace = xamlNamespace;
  42.                     return true;
  43.                 }
  44.                 return base.TryGetCompatibleXamlNamespace(xamlNamespace, out compatibleNamespace);
  45.             }
  46.         }
  47.         class CustomWriter : XamlObjectWriter
  48.         {
  49.             public bool IsDesignMode { get; set; }
  50.             private static PropertyInfo DataContextProperty = typeof(Foo).GetProperty("DataContext");
  51.             public CustomWriter(XamlSchemaContext schemaContext) : base(schemaContext)
  52.             {
  53.             }
  54.  
  55.             public override void WriteStartMember(XamlMember property)
  56.             {
  57.                 if (IsDesignMode && property == DesignContextDirective)
  58.                     base.WriteStartMember(new XamlMember(DataContextProperty, SchemaContext));
  59.                 else
  60.                     base.WriteStartMember(property);
  61.             }
  62.         }
  63.  
  64.         static void Main(string[] args)
  65.         {
  66.             foreach (var designMode in new[] {true, false})
  67.             {
  68.  
  69.                 XamlXmlReaderSettings settings = new XamlXmlReaderSettings {LocalAssembly = typeof(Foo).Assembly};
  70.                
  71.                 var rdr = new XamlXmlReader(new StringReader(@"
  72. <Foo xmlns='clr-namespace:Sandbox'
  73. xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
  74. xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
  75. mc:Ignorable='d'
  76. d:DataContext='wat'>
  77. </Foo>"), new CustomContext(){IsDesignMode = designMode}, settings);
  78.  
  79.                 var writer = new CustomWriter(rdr.SchemaContext){IsDesignMode = designMode};
  80.                 XamlServices.Transform(rdr, writer);
  81.  
  82.                 var obj = (Foo) writer.Result;
  83.                 Console.WriteLine($"DesignMode: {designMode}, DataContext: {obj.DataContext}");
  84.             }
  85.  
  86.         }
  87.  
  88.  
  89.  
  90.  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement