How to run a XAML file?

Hi,

I have a WPF xaml file and I’d like to execute it using IronRuby. I know
I can use the System::Windows::Markup::XamlReader.parse method, but is
there another way?

Using the XamlReader class makes registering events, for example, kind
of an irritating task.

Thanks,
Shay.

Shay F. wrote:

Hi,

I have a WPF xaml file and I’d like to execute it using IronRuby. I know
I can use the System::Windows::Markup::XamlReader.parse method, but is
there another way?

Using the XamlReader class makes registering events, for example, kind
of an irritating task.

Thanks,
Shay.

Yes, there is a ‘Wpf.rb’ in your \Samples\Tutorial directory (This
should exist for you if you have a source copy, or the regular 0.6
release)

This adds a number of helpers to make working with XAML easier.
I haven’t looked at it extensively, but I’d say you can probably learn
what is doing by running the wpf tutorial in that dir, and following the
source.
Look for the “load” methods associated with xaml to get an idea of what
helpers are added.

Also, if you are trying to do something simple, and the XAML is actually
getting in the way, Thibaut Barrère’s “magic” looks to be a very nice
DSL for this:

http://github.com/thbar/magic/tree/master

I was looking through the mailing list archives for unanswered mails …
eek, sorry Shay! Well, if you still have this question …

You can also load XAML with System::Window::Application.LoadComponent:

This loads “foo.xaml” into the “canvas” variable

include System
include System::Windows
include System::Windows::Controls
canvas = Canvas.new
Application.load_component canvas, Uri.new(“foo.xaml”, UriKind.relative)

This only works when foo.xaml has a x:Class equal to the type passed to
the first arg to load_component, like
x:Class=“System.Windows.Controls.Canvas”.

~Jimmy

Thanks Jimmy :slight_smile:

On Mon, Nov 16, 2009 at 5:32 AM, Jimmy S. <

-----Original Message-----
From: [email protected]
Jimmy S.
Sent: Monday, November 16, 2009 5:32 AM

This loads “foo.xaml” into the “canvas” variable include

System include System::Windows include
System::Windows::Controls canvas = Canvas.new
Application.load_component canvas, Uri.new(“foo.xaml”,
UriKind.relative)

I was looking for ways to load XAML to a custom class from a file at
runtime in the way described above. I have load_component.rb:

require ‘PresentationFramework, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35’
require ‘PresentationCore, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35’
require ‘WindowsBase, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35’

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

canvas = Canvas.new
Application.load_component(canvas, Uri.new(“foo.xaml”,
UriKind.relative) )

I have my foo.xaml file in the same location as the ruby script above.
When I run the script with “ir load_component.rb” I get error:

PresentationFramework:0:in GetStreamCore': Cannot locate resource 'foo.xaml'. (IOError) from WindowsBase:0:inGetStream’
from WindowsBase:0:in GetStream' from PresentationFramework:0:inLoadComponent’
from loadcomponent.rb:0

Where should I put the foo.xaml for the script above to find it? Only
examples I found from Application.LoadComponent from MSDN were using
relative URI:s referring to XAML-files embedded in some assembly. I want
to load a XAML-file on a disk however. XamlReader.Load is not enough for
me since I need to have custom classes created with the xaml:class="…"
attribute.

Robert B.