开发者

Import and render the contents of a XAML file inline in another XAML file?

开发者 https://www.devze.com 2022-12-24 02:16 出处:网络
I have a XAML file that I exported from Expression Design.I would like to render that XAML content inline in one of my user controls.Can I reference the external XAML file that I want to include as a

I have a XAML file that I exported from Expression Design. I would like to render that XAML content inline in one of my user controls. Can I reference the external XAML file that I want to include as a Resource on the UserCon开发者_运维技巧trol I want to render it in? Or is there some other markup that I can use to identify the XAML object in my project that I want rendered in the current location?

I am using Silverlight 4.


You can include the XAML as content or as a file on the web server and use XamlReader.Load to dynamically load and create Xaml content. There's not a control that can do what you want directly (but it would be simple to wrap the functionality described in the link).

For example:

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Path x:Name="top" Width="24" Height="12" Stretch="Fill" Fill="#FF065F02" Stroke="#10000000" Data="F1 M 0,6L 12,12L 24,6L 12,0L 0,6 Z "/>
</Canvas>

Then, using whatever your favorite trick is for loading Content, get the above string and Load it:

Canvas c = XamlReader.Load(myXaml) as Canvas;

Then, add the canvas as a child to the parent control (or whatever the type is you want to use, as it doesn't need to be a Canvas).

var e = from a in XDocument.Load("resources.xml").Descendants("assets") 
  where (string) a.Attribute("id") == desiredId select a.FirstNode;

My Xaml in the above case was in a file marked as "Content" and used a XDocument to load it based on an ID (My XML document has multiple free floating assets all tagged with an ID):

<assets>
  <asset id="top">
    <Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Path x:Name="top" Width="24" Height="12" Stretch="Fill" Fill="#FF065F02" Stroke="#10000000" Data="F1 M 0,6L 12,12L 24,6L 12,0L 0,6 Z "/>
    </Canvas>
  </a>
...


If it's just a bunch of stuff from the Resources section of a user control or grid, control templates or data templates, you could use merged resource dictionaries to include it.

Otherwise, if it's actual XAML content, you need to either put it all in a user control or content control, which could be included - but you can't directly include the XAML file, you'll need to copy and paste it in.

0

精彩评论

暂无评论...
验证码 换一张
取 消