Example of an IronRuby Silverlight application

The code in Listing 11.8 contains IronRuby code that implements all of the techniques that have been discussed in this section. The full IronRuby Silverlight application comprises the Web page in Listing 11.1, the XAML in Listing 11.6, the manifest in Listing 11.7, as well as the Ruby code in Listing 11.8.

LISTING 11.8

Example app.rb File for an IronRuby Silverlight Application require 'System.Windows.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3 85 6ad3 64e3 5'

require 'System.Windows.Controls.Extended, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3 85 6ad3 64e3 5'

include System::Windows include System::Windows::Controls

@xaml = Application.current.load_root_visual(UserControl.new, "app.xaml")

def addltem(name)

panel = StackPanel.new panel.margin = Thickness.new 5 panel.Orientation = Orientation.horizontal label = TextBlock.new label.Width = 80

label.TextAlignment = TextAlignment.right label.Text = name tBox = TextBox.new tBox.Width = 200

panel.children.add(label)

panel.children.add(tBox)

return panel end def onClick(sender, args) panel = StackPanel.new panel.margin = Thickness.new 50 panel.children.add(self.addItem("First")) panel.children.add(self.addItem("Last")) panel.children.add(self.addItem("Address")) panel.children.add(self.addItem("Phone")) panel.children.add(self.addItem("Email")) @xaml.rubyForm.children.add(panel) sender.content = 'Clicked'

end class FrameworkElement def method_missing(method)

find_name(method.to_s.to_clr_string)

end end

@xaml.titleBlock.Text = 'Dynamic IronRuby Form'

@xaml.find_name('myButton').click{|sender, args| onClick(sender, args)}

The XAML code in Listing 11.6 defines a Button control named myButton, a TextBlock control named titleBlock, and a Canvas control named rubyForm. The code in Listing 11.8 accesses these controls to implement an event handler and dynamic XAML to create a simple Silverlight Web form.

The code first adds references to the Controls and Controls.Extended libraries. Then it imports the necessary namespaces required by the application using required statements.

To access the Silverlight XAML, the IronRuby code uses the Application.current.load_ root_visual() function to get the UserControl control from the app.xaml file using the following line of code.

Application.current.load_root_visual(UserControl.new,"app.xaml")

The addItem() function accepts a single argument name. It then creates a StackPanel, TextBlock, and TextBox object. It then sets the necessary attributes for the controls. The Text property of the TextBox control is set to the value of the name argument. Notice that the TextAlignment and Orientation properties are set using TextAlignment and Orientation objects using the following code:

panel.Orientation = Orientation.horizontal label.TextAlignment = TextAlignment.right

The TextBlock and TextBox objects are added to the StackPanel using the following lines of code, and the StackPanel is returned by the function:

panel.children.add(label) panel.children.add(tBox)

Using IronRuby, the namescope is not directly linked up. Therefore you cannot use dot syntax to access elements that have been named in the namescope. To solve this problem, the code in Listing 11.8 overrides the method_missing() method of the FrameworkElement class so that the method_missing() method implements a find_name() method to get the name as shown in the following code snippet:

class FrameworkElement def method_missing(method)

find_name(method.to_s.to_clr_string)

end end

Without overridding the method_missing() method, you have to access items in the name-scope using the following syntax:

@xaml.find_name(titleBlock).Text = 'Welcome to Dynamic IronRuby'

You can use dot syntax to access the namescope names if you do override the method_ missing() method. The following code shows an example of using the UserControl object xaml to modify the Text attribute of a TextBlock that is defined in app.xaml:

Application.current.load_root_visual(UserControl.new,"app.xaml") @xaml.titleBlock.Text = 'Welcome to Dynamic IronRuby'

The code in Listing 11.8 implements a Click event handler to dynamically build a Web form by calling the addItem() function in the onClick() event handler function. Notice that in the onClick() event handler function, the Content property of the Button control that triggered the event will be modified using the following code:

sender.content = 'Clicked'

The onClick() event handler is attached to the Button control myButton using the following line of code:

@xaml.find_name('myButton').click{|sender, args| onClick(sender, args)}

The result is that the Silverlight application creates a Web page similar to the one in Figure 11.4. Clicking the Populate button dynamically generates a Silverlight Web form.

FIGURE 11.4

An IronRuby Silverlight application that dynamically generates a Silverlight Web form

Silverlight DLR Application

Dynamic IronRuby Form

[ Populate ] .

Silverlight DLR Application

Dynamic IronRuby Form

Firstj

Lastj

Addressj

Phonej

Emailj

0 -2

Average user rating: 1 stars out of 2 votes

Post a comment

  • Receive news updates via email from this site