Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Loading XAML files Dynamically
11 replies. Latest Post by sakthisaisaranyan on September 14, 2007.
(1)
helfon
Member
48 points
34 Posts
05-29-2007 11:59 AM |
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!
car =
luisabreu
Participant
1676 points
612 Posts
05-29-2007 1:42 PM |
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
458 points
123 Posts
05-29-2007 2:28 PM |
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.
coreyper...
4 points
2 Posts
05-29-2007 4:33 PM |
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
1084 points
249 Posts
05-29-2007 4:52 PM |
This is how my controls do it:
_root =
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.
05-29-2007 5:29 PM |
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"
05-30-2007 12:14 PM |
Thanks Dave! Worked like a charm.
05-30-2007 12:17 PM |
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.
sakthisa...
336 points
138 Posts
09-06-2007 5:15 AM |
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
09-07-2007 4:59 PM |
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.
09-14-2007 4:39 AM |
Hi Helfon,
Thanks. But still the xaml does not load and the class is recursively called. I do not know the reason behind this.
09-14-2007 5:44 AM |
I found the problem. I did not remove the Loaded event and the x:Class from the xaml file.
Sakthi