代码之家  ›  专栏  ›  技术社区  ›  Dan Auclair

在VisualState中修改Silverlight资源

  •  1
  • Dan Auclair  · 技术社区  · 16 年前

    我想知道是否有一种简单的方法可以在不同的视觉状态之间修改控件的某种共享资源(即画笔)。例如,我想定义一个画笔,用作边框的背景和不同矩形的填充。在另一个VisualState中,我希望在一个位置(资源)更改此背景画笔,并将其反映在使用该资源的所有元素中。

    我不确定是否可以通过VisualState中故事板的targetname的名称(而不是键)真正引用资源。

    以下是我在XAML中尝试执行的操作的简化示例:

    <UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SilverlightApplication.MainPage"
    Width="200" Height="200">
    <UserControl.Resources>
        <SolidColorBrush x:Name="Background" x:Key="Background" Color="Black" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="MyStates">
                <VisualState x:Name="Normal"/>
                <VisualState x:Name="Red">
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Color)">
                            <EasingColorKeyFrame KeyTime="00:00:00" Value="Red"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Border Background="{StaticResource Background}" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Red" BorderThickness="1"/>
        <Rectangle Fill="{StaticResource Background}" Width="100" Height="100" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
    </Grid>
    </UserControl>
    

    我有一种感觉,因为这些是Silverlight中的静态资源,它们只加载一次,不能更改。我知道WPF有一些动态资源的概念。是否有任何方法可以在Silverlight中实现这种行为,而不必在所有元素中重新定义画笔?

    1 回复  |  直到 16 年前
        1
  •  0
  •   WiredPrairie    16 年前

    不幸的是,动态资源不存在于Silverlight中。

    有些人使用绑定。

    有一种方法可以在一个用户控件内模拟您可能想要尝试的体验。

    下面代码中发生的所有事情都是,故事板对单个矩形的填充进行动画处理,然后使用元素绑定将其绑定到用户控件中的其他填充。源矩形不需要可见即可工作。

    <UserControl x:Class="TestSilverlightStuff.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestSilverlightStuff"            
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        <UserControl.Resources>
            <SolidColorBrush x:Key="Background" Color="Black" />
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="MyStates">
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="Red">
                        <Storyboard>
                            <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="trickyRectangle" d:IsOptimized="True"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Rectangle Fill="Black" x:Name="trickyRectangle" Visibility="Collapsed" />
            <Border Background="{Binding Fill, ElementName=trickyRectangle}" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Red" BorderThickness="1"/>
            <Rectangle Fill="{Binding Fill, ElementName=trickyRectangle}" Width="100" Height="100" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
            <Button Content="Button" Height="57" HorizontalAlignment="Left" Margin="12,231,0,0" Name="button1" VerticalAlignment="Top" Width="153" Click="button1_Click" />
        </Grid>
    </UserControl>
    

    这是C按钮单击代码:

    private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        VisualStateManager.GoToState(this, "Red", true);
    }
    

    它不像一个活力源泉那么优雅,但在某些情况下也能发挥作用。

    推荐文章