Jirapong (owner)

Fork Of

gist: 172826 by Ball XamlTools

Revisions

gist: 199891 Download_button fork
public
Public Clone URL: git://gist.github.com/199891.git
Embed All Files: show embed
xamltools.rb #
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
require 'rubygems'
require 'builder'
require 'PresentationCore'
require 'PresentationFramework'
require 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeytoken=b77a5c561934e089'
 
module XamlTools
  include System::Windows
  StringReader = System::IO::StringReader;
  XamlReader = System::Windows::Markup::XamlReader
  XmlReader = System::Xml::XmlReader;
  
  class System::Windows::Window
    def [](arg)
      self.find_name(arg.to_s)
    end
  end
  class Xaml
    def self.add_xaml_ns(hash)
      if not hash.key? "xmlns:x"
        hash["xmlns:x"] = "http://schemas.microsoft.com/winfx/2006/xaml"
      end
    end
    def self.add_presentation_ns(hash)
      if not hash.key? :xmlns
        hash[:xmlns] = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      end
    end
    def self.create(hash, &block)
      add_presentation_ns hash
      add_xaml_ns hash
      xaml = Builder::XmlMarkup.new(:indent => 2)
      yield(xaml)
      XamlReader.load(XmlReader.create(StringReader.new(xaml.target!.to_clr_string)))
    end
    def self.page(hash, &block)
      create(hash) do |xaml|
        xaml.Page(hash, &block)
      end
    end
    def self.user_control(hash, &block)
      create(hash) do |xaml|
        xaml.UserControl(hash, &block)
      end
    end
    def self.window(hash, &block)
      create(hash) do |xaml|
        xaml.Window(hash, &block)
      end
    end
  end
end