在Silverlight 2.0中重用StaticResource

时间:2008-09-19 13:59:31

标签: silverlight static-resource

我目前正在使用Silverlight 2.0 Beta 2进行测试,我的目标是定义一次资源元素,然后在渲染中多次重复使用它。这个简单的例子将一个矩形(myRect)定义为一个资源,然后我尝试重复使用它两次 - 它因错误而失败:

属性{StaticResource myRect}值超出范围。 [线:9位置:83]

顺便说一下,这个示例在WPF中运行良好。

<UserControl x:Class="ReuseResourceTest.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="200" Height="200">
    <Canvas x:Name="LayoutRoot" Background="Yellow">
        <Canvas.Resources>
            <RectangleGeometry x:Key="myRect" Rect="25,50,25,50" />
        </Canvas.Resources>
        <Path Stroke="Black" StrokeThickness="10" Data="{StaticResource myRect}" />
        <Path Stroke="White" StrokeThickness="4"  Data="{StaticResource myRect}" />
    </Canvas>
</UserControl>

关于这里有什么的想法。

谢谢,

- Ed

1 个答案:

答案 0 :(得分:2)

在尝试重用定义为静态资源的组件时,我也遇到了同样的问题。我找到的解决方法不是将控件声明为资源,而是定义样式设置所需的所有属性,并在每次需要时使用该样式实例化新控件。

编辑:当您将控件分配给已在另一个容器内的容器时,会发生超出范围的异常。它也发生在许多其他场景中(例如将样式应用于已经有样式的对象),但我相信这是你的情况。