在ResourceDictionary文件中使用viewbox

时间:2013-07-10 08:45:29

标签: c# windows-phone-7 resourcedictionary viewbox

我有ResourceFile1.xaml文件 其内容

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Viewbox x:Key="Flash-On" >
    <Grid  Width="256" Height="256" Visibility="Visible">
        <Path Tag="Icon" Data="F1M376.251,632.755L385.665,632.755 381.302,646.07 394.618,646.07 389.11,660.302 393.01,660.302 381.531,672.93 377.398,660.763 381.073,660.763 383.829,652.268 369.825,652.268 376.251,632.755z" Stretch="Uniform" Fill="#FFFFFFFF" Width="176" Height="176" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
            <Path.RenderTransform>
                <TransformGroup>
                    <TransformGroup.Children>
                        <RotateTransform Angle="0" />
                        <ScaleTransform ScaleX="1" ScaleY="1" />
                    </TransformGroup.Children>
                </TransformGroup>
            </Path.RenderTransform>
        </Path>
    </Grid>
</Viewbox>


<Viewbox x:Key="Flash-Off">
            ....
</Viewbox>
</ResourceDictionary>

PhoneAppplicationPage的代码

    <phone:PhoneApplicationPage.Resources>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceFile1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</phone:PhoneApplicationPage.Resources>

但这个代码不起作用。 Designer Error:InvalidOperationException:Element已经是另一个元素的子元素。 还<Button Content="{StaticResource Flash-On}"/>如果我使用类似的代码 运行时错误:无法分配给属性'System.Windows.Controls.ContentControl.Content'。 如果Viewbox在Grid.Resources中使用没有问题,但是我想和ResourceDictionary一起使用。我可以吗?

1 个答案:

答案 0 :(得分:6)

因为Silverlight resource dictionary内的所有内容都必须是可共享的。在WPF中,您可以对资源字典中的对象使用x:Shared属性来强制WPF为每个资源检索创建新实例。 要在Silverlight中避免这种情况,您可以创建DataTemplate

<DataTemplate x:Key="ButtonTemplate">
    <Viewbox>
<!-- Here your content-->
    </Viewbox>
</DataTemplate>

<Button ContentTemplate="{StaticResource ButtonTemplate}"/>

更新0

我写了一个示例,它更改了取决于CheckBox的值的模板。

转换器来更改模板:

public class TemplateSelectorConverter : IValueConverter
{
    public DataTemplate TrueTemplate { get; set; }

    public DataTemplate FalseTemplate { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (((bool) value))
            return TrueTemplate;
        return FalseTemplate;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

资源:

<DataTemplate x:Key="FirstTemplate">
    <TextBox Text="FirstTemplate" />
</DataTemplate>

<DataTemplate x:Key="SecondTemplate">
    <TextBox Text="SecondTemplate" />
</DataTemplate>

<internal:TemplateSelectorConverter x:Key="TemplateSelector" TrueTemplate="{StaticResource FirstTemplate}"
                                    FalseTemplate="{StaticResource SecondTemplate}" />

Xaml标记:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <CheckBox Name="CheckBox"/>
    <Button Grid.Column="1"
            ContentTemplate="{Binding Path=IsChecked, ElementName=CheckBox, Converter={StaticResource TemplateSelector}}"
            VerticalAlignment="Top" />
</Grid>

我希望这会有所帮助。