Parsing error when loading XAML dynamically
Last post 05-12-2008 2:43 AM by swildermuth. 1 replies.
Sort Posts:
05-11-2008 1:43 AM
Parsing error when loading XAML dynamically

I have rec.xaml set as an embedded resource.   Here is its XAML:

<UserControl x:Class="SL.rec"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Rectangle Stroke="Red" Fill="Blue"/>
    </Grid>
</UserControl>

In the main page, page.xaml.cs, I have:

string rec;
Stream s = this.GetType().Assembly.GetManifestResourceStream("SL.rec.xaml");     
rec = new StreamReader(s).ReadToEnd();

Rectangle theRectangle = (Rectangle)XamlReader.Load(rec);

I get this error on the above line:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll
Additional information: AG_E_UNKNOWN_ERROR [Line: 4 Position: 29]

I'd like to load the rectangle into the page.xaml, which looks like this:

 <UserControl x:Class="SL.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300" x:Name="_root">
    <Grid x:Name="LayoutRoot" Background="White">
     <Button HorizontalAlignment="Stretch" VerticalAlignment="Top" Content="Click Here" Height="50" x:Name="_button" Click="_button_click"/>    </Grid>
</UserControl>

Any suggestions?

brettr

Joined on 12-07-2006
Seattle
Posts 69
05-12-2008 2:43 AM
Marked as Answer
Re: Parsing error when loading XAML dynamically

The problem is that XamlReader can't do two things you're asking it to.  It can't start with a UserControl and can't have a backing class.  If you move the xmlns and xmlns:x to the Grid and strip the UserControl it will work.

(If this has answered your question, please "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 2 Workshop
May 12-14, 2008 - Atlanta, GA
May 28-30, 2008 - San Francisco, CA
http://www.silverlight-tour.com

swildermuth

Joined on 10-13-2003
Atlanta, GA
Posts 1,058