# How to run a XAML file?

**URL:** <https://www.ruby-forum.com/t/how-to-run-a-xaml-file/172638>\
**Category:** IronRuby\
**Created:** [July 24, 2009, 11:33am UTC](https://www.ruby-forum.com/t/how-to-run-a-xaml-file/172638 "2009-07-24T11:33:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shayfriedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@shayfriedman](https://www.ruby-forum.com/u/shayfriedman)\
**Post date:** [July 24, 2009, 11:33am UTC](https://www.ruby-forum.com/t/how-to-run-a-xaml-file/172638/1 "2009-07-24T11:33:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shayfriedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@shayfriedman](https://www.ruby-forum.com/u/shayfriedman)\
**Post date:** [July 24, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/how-to-run-a-xaml-file/172638/2 "2009-07-24T17:38:40Z")

</div>

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:

> **[GitHub - thbar/magic: IronRuby sugar for WPF, Silverlight and Windows Forms.](https://github.com/thbar/magic/tree/master)**
>
> master

---

<div class="post-metadata">

**Author:** ![shayfriedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@shayfriedman](https://www.ruby-forum.com/u/shayfriedman)\
**Post date:** [November 16, 2009, 4:32am UTC](https://www.ruby-forum.com/t/how-to-run-a-xaml-file/172638/3 "2009-11-16T04:32:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shayfriedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@shayfriedman](https://www.ruby-forum.com/u/shayfriedman)\
**Post date:** [November 16, 2009, 6:53am UTC](https://www.ruby-forum.com/t/how-to-run-a-xaml-file/172638/4 "2009-11-16T06:53:23Z")

</div>

Thanks Jimmy 🙂

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

---

<div class="post-metadata">

**Author:** ![shayfriedman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@shayfriedman](https://www.ruby-forum.com/u/shayfriedman)\
**Post date:** [June 3, 2010, 2:17pm UTC](https://www.ruby-forum.com/t/how-to-run-a-xaml-file/172638/5 "2010-06-03T14:17:45Z")

</div>

> -----Original Message-----  
> From: removed\_email\_address@domain.invalid  
> 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:in`GetStream’  
from WindowsBase:0:in `GetStream' from PresentationFramework:0:in`LoadComponent’  
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.
