Skip to content

Instantly share code, notes, and snippets.

@Ball
Created August 15, 2009 17:20
Show Gist options
  • Save Ball/168408 to your computer and use it in GitHub Desktop.
Save Ball/168408 to your computer and use it in GitHub Desktop.
With Xaml
require 'rubygems'
require 'builder'
require 'PresentationCore'
require 'PresentationFramework'
require 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeytoken=b77a5c561934e089'
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.window(hash, &block)
create(hash) do |xaml|
xaml.Window(hash, &block)
end
end
end
window = Xaml.window( :Title => "Lef' Frontal Lobe",
:Width => 450, :SizeToContent => :Height) do |w|
w.StackPanel :Margin => 15 do |layout|
layout.StackPanel(:Orientation => :Horizontal) do |search_control|
search_control.TextBox(:Width => 390, "x:Name" => "SearchBox" )
search_control.Button(:Content => "Go", "x:Name" => "SearchButton")
end
layout.Label(:FontSize => 24, :Content => "Results",
:HorizontalAlignment => "Center", :Margin => 5 )
layout.ListBox("x:Name" => "ResultsList")
end
end
window[:SearchButton].click do |sender, args|
items = window[:ResultsList].items
items.clear
items.add window[:SearchBox].text
end
Application.new().run(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment