Skip to main content

Microsoft Silverlight

Answered Question Loading XAML files DynamicallyRSS Feed

(1)

helfon
helfon

Member

Member

48 points

34 Posts

Loading XAML files Dynamically

How do you load a XAML file that is contained within the same project?  I am trying this below where car.xaml exists in my current project but I cannot seem to find the correct path.  I have tried the full path and many other relative variations.   Thanks!

 

string s = "";

using (StreamReader rdr = File.OpenText("car.xaml"))
{
s = rdr.ReadToEnd();
}

car = XamlReader.Load(s) as Canvas;

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Loading XAML files Dynamically

hello.

i think that you cannot do that because the OpenText method is marked as SecurityCritical (which means that it can only be called from platform code). if you need to open files on the user pc, you can only do that by using the file dialog. If you can embedt the xaml file, then do that and get it from the resources (to see how to do that, take a look at the controls sample that is available).

Vivek Dalvi
Vivek Dalvi

Member

Member

458 points

123 Posts

Microsoft

Re: Loading XAML files Dynamically

that is correct. Either you need to use fileopen dialog that lets user pick the file and returns stream to application or you could also embed this file in assembly as resource and just read it (similar to what a custom control does in its constructor.

Vivek Dalvi
Program Manager
Microsoft
This post is provided "as-is"

coreyperkins_25
coreyper...

Member

Member

4 points

2 Posts

Re: Loading XAML files Dynamically

Vivek,

 Could you provide a link or some code to display what is done in the constructor of a custom control?  I've tried embedding a xaml file in my assembly and using System.Uri and System.Windows.Application.LoadComponent(uri) to load the xaml file, however, I receive an error that states the resource cannot be found despite the fact that my xaml file is in the same subfolder as the class calling the code.

                 Uri uri = new Uri(@"/Navigation/file.xaml", UriKind.Relative);
                FlowDocument flowDoc = System.Windows.Application.LoadComponent(uri) as FlowDocument;

 

Corey

Dave Relyea
Dave Relyea

Participant

Participant

1084 points

249 Posts

Microsoft
Answered Question

Re: Loading XAML files Dynamically

This is how my controls do it:

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("agLayoutDemo.Controls.Button.xaml");

 

_root = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd()) as Canvas;

The XAML file can be a Silverlight Page (which sometimes does not appear to work properly) or an Embedded Resource. In the above example, "agLayoutDemo" is the default namespace of the project.

helfon
helfon

Member

Member

48 points

34 Posts

Re: Re: Loading XAML files Dynamically

Thank you very much Dave,  that did it.  Is there any reason that adding the item I retrieve from XAML multiple times to the parent canvas should cause an exception?  I read the Canvas in, add a uniquely named TranslateTransform to it and then add it to the Parent Canvas.  The first one I do works fine but the second gives me the exception "Value does not fall within the expected range" 

coreyperkins_25
coreyper...

Member

Member

4 points

2 Posts

Re: Re: Loading XAML files Dynamically

Thanks Dave!  Worked like a charm.

helfon
helfon

Member

Member

48 points

34 Posts

Re: Re: Loading XAML files Dynamically

I also managed to work out my issue of adding the same XAML I had just loaded multiple times.  I had the Canvas and every sub object named with an x:Name.  Removing all of the x:Name properties allowed me to add it as many times as I like and got rid of the "Value does not fall within the expected range" exception.   Thanks again for the help.

sakthisaisaranyan
sakthisa...

Member

Member

336 points

138 Posts

Re: Re: Loading XAML files Dynamically

Hi Dave,

I have taken the source code of agControls from your block and copied it to my project. But when i use your text box control, the 'Value does not fall within expected range exception' occurs in the InitializeFromXaml method present in the LayoutControl's constructor. Please help me.

 Regards,

Sakthi Sai Saranyan

helfon
helfon

Member

Member

48 points

34 Posts

Re: Re: Loading XAML files Dynamically

Try this when you load your XAML

            string xaml;
            //Pull the xaml file out of the Resources
            System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("Resources.file.xaml");
            xaml = new System.IO.StreamReader(s).ReadToEnd();

            actualControl = XamlReader.Load(xaml, true) as Canvas;

 The second property that I set to "true" in XamlReader.Load above is createNamescope.  Setting this value to true creates all the children objects in your XAML file as their own private namespace instead of in the namespace of everything else on your page.  That is what I was referring to as my problem with conflicting x:Name properties above.  

sakthisaisaranyan
sakthisa...

Member

Member

336 points

138 Posts

Re: Re: Loading XAML files Dynamically

Hi Helfon,

Thanks. But still the xaml does not load and the class is recursively called. I do not know the reason behind this.

Regards,

Sakthi Sai Saranyan

sakthisaisaranyan
sakthisa...

Member

Member

336 points

138 Posts

Re: Re: Loading XAML files Dynamically

Hi Helfon,

I found the problem. I did not remove the Loaded event and the x:Class from the xaml file.

Regards,

Sakthi

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities