将 PointAnimation 的 TargetName 设置为资源名称会引发 InvalidOperationException (XAML/Silverlight)

发布于 2024-10-13 02:00:23 字数 2314 浏览 6 评论 0原文

我有一个 Button 控件,我正在为其定义自定义模板。使用一些 Storyboard,我可以使用 DoubleAnimation 成功操作控件属性、命名变换和效果;使用 PointAnimation 时出现了问题,根据 VS,会抛出 InvalidOperationException无法解析 TargetName OverlayEllipse。

动画已启动在代码中,简单地说:

private void Button_MouseEnter(object sender, MouseEventArgs e)
{
    PopUpStoryboard.Begin();
}

相关的 XAML:

<Button ...>    
    <Button.Resources>
        <RadialGradientBrush 
            x:Name="OverlayBrush" ...>
            <RadialGradientBrush.GradientStops>
                ...
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
        <Storyboard x:Name="PopUpStoryboard">
            ...
            <PointAnimation
                Storyboard.TargetName="OverlayEllipse"
                Storyboard.TargetProperty="(Shape.Fill)(RadialGradientBrush.GradientOrigin)"
                Duration="0:0:.1"
                To="0.9,1.2"/>
        </Storyboard>
    <Button.Resources>
    <Button.Style>
        <Style TargetType="Button">
            ...
            <Setter Property="Template">
                <Setter.Value>     
                    <ControlTemplate>
                        <Grid>
                            ...                           
                            <Ellipse x:Name="OverlayEllipse" Fill="{StaticResource OverlayBrush}"/>
                            ...
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

Ellipse 显然可以解析资源,因为它显示得很好,并且直接从 Storyboard.Begin 调用抛出异常。如何更正我所拥有的内容,以便 Storyboard 能够按名称解析资源?

我曾想过使用 StaticResource 绑定,但是,考虑到它直接引用对象而不是其名称,这是行不通的。我只是尝试使用 StaticResource 绑定设置 Target,而不是 TargetName - 这给了我一个构建错误(很奇怪?)说明:名称空间中的类型“PointAnimation”上不存在属性“Target”http://schemas.microsoft.com/winfx/2006/xaml/presentation'。

谢谢。

I have a Button control for which I'm defining a custom template. Using some Storyboard's I can successfully manipulate control properties, named transforms and effects with DoubleAnimation's; the problem has arisen, when using a PointAnimation, that an InvalidOperationException is thrown as, per VS: Cannot resolve TargetName OverlayEllipse.

The animation is initiated in code, simply:

private void Button_MouseEnter(object sender, MouseEventArgs e)
{
    PopUpStoryboard.Begin();
}

And the relevant XAML:

<Button ...>    
    <Button.Resources>
        <RadialGradientBrush 
            x:Name="OverlayBrush" ...>
            <RadialGradientBrush.GradientStops>
                ...
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
        <Storyboard x:Name="PopUpStoryboard">
            ...
            <PointAnimation
                Storyboard.TargetName="OverlayEllipse"
                Storyboard.TargetProperty="(Shape.Fill)(RadialGradientBrush.GradientOrigin)"
                Duration="0:0:.1"
                To="0.9,1.2"/>
        </Storyboard>
    <Button.Resources>
    <Button.Style>
        <Style TargetType="Button">
            ...
            <Setter Property="Template">
                <Setter.Value>     
                    <ControlTemplate>
                        <Grid>
                            ...                           
                            <Ellipse x:Name="OverlayEllipse" Fill="{StaticResource OverlayBrush}"/>
                            ...
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

The Ellipse can resolve the resource, obviously, as it displays just fine and the exception is thrown directly from the Storyboard.Begin call. How can I correct what I have in order for the Storyboard to be able to resolve to resource by name?

I had thought of using StaticResource binding, however, that won't work considering it refers directly to the object, not its name. I just tried to set the Target, as opposed to TargetName, using the StaticResource binding - this gives me a build error (strangely enough?) stating: The property 'Target' does not exist on the type 'PointAnimation' in the namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2024-10-20 02:00:23

仍然以控件(椭圆)上的属性为目标,而不是以资源为目标。添加 (Shape.Fill) 以定位 GradientOrigin

<PointAnimation
  Storyboard.TargetName="OverlayEllipse"
  Storyboard.TargetProperty="(Shape.Fill).(RadialGradientBrush.GradientOrigin)"
  Duration="0:0:.1"
  To="0.9,1.2"/>

编辑:这是一个完整的示例按钮样式。

<Style x:Key="ButtonStyle1" TargetType="Button">
  <Setter Property="Background" Value="#FF1F3B53"/>
  <Setter Property="Foreground" Value="#FF000000"/>
  <Setter Property="Padding" Value="3"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="BorderBrush">
    <Setter.Value>
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFA3AEB9" Offset="0"/>
        <GradientStop Color="#FF8399A9" Offset="0.375"/>
        <GradientStop Color="#FF718597" Offset="0.375"/>
        <GradientStop Color="#FF617584" Offset="1"/>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <Grid.Resources>
            <RadialGradientBrush x:Key="Brush1" GradientOrigin="0.5,0.5">
                    <GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="#F9FFFFFF" Offset="0.375"/>
                    <GradientStop Color="#E5FFFFFF" Offset="0.625"/>
                    <GradientStop Color="#C6FFFFFF" Offset="1"/>
                  </RadialGradientBrush>
          </Grid.Resources>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal"/>
              <VisualState x:Name="MouseOver">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>
                  <PointAnimation Duration="0" To="0,0.5" Storyboard.TargetProperty="(Shape.Fill).(RadialGradientBrush.GradientOrigin)" Storyboard.TargetName="BackgroundGradient" d:IsOptimized="True"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"/>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FocusStates">
              <VisualState x:Name="Focused">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Unfocused"/>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
            <Grid Background="{TemplateBinding Background}" Margin="1">
              <Border x:Name="BackgroundAnimation" Background="#FF448DCA" Opacity="0"/>
              <Rectangle x:Name="BackgroundGradient" Fill="{StaticResource Brush1}">
              </Rectangle>
            </Grid>
          </Border>
          <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
          <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" IsHitTestVisible="false" Opacity="0" RadiusY="3" RadiusX="3"/>
          <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" Opacity="0" RadiusY="2" RadiusX="2" Stroke="#FF6DBDD1" StrokeThickness="1"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Instead of targeting the resource, still target the property on the control (the Ellipse). Add (Shape.Fill) to target the GradientOrigin.

<PointAnimation
  Storyboard.TargetName="OverlayEllipse"
  Storyboard.TargetProperty="(Shape.Fill).(RadialGradientBrush.GradientOrigin)"
  Duration="0:0:.1"
  To="0.9,1.2"/>

Edit: Here's a complete sample button style.

<Style x:Key="ButtonStyle1" TargetType="Button">
  <Setter Property="Background" Value="#FF1F3B53"/>
  <Setter Property="Foreground" Value="#FF000000"/>
  <Setter Property="Padding" Value="3"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="BorderBrush">
    <Setter.Value>
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFA3AEB9" Offset="0"/>
        <GradientStop Color="#FF8399A9" Offset="0.375"/>
        <GradientStop Color="#FF718597" Offset="0.375"/>
        <GradientStop Color="#FF617584" Offset="1"/>
      </LinearGradientBrush>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid>
          <Grid.Resources>
            <RadialGradientBrush x:Key="Brush1" GradientOrigin="0.5,0.5">
                    <GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="#F9FFFFFF" Offset="0.375"/>
                    <GradientStop Color="#E5FFFFFF" Offset="0.625"/>
                    <GradientStop Color="#C6FFFFFF" Offset="1"/>
                  </RadialGradientBrush>
          </Grid.Resources>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal"/>
              <VisualState x:Name="MouseOver">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>
                  <PointAnimation Duration="0" To="0,0.5" Storyboard.TargetProperty="(Shape.Fill).(RadialGradientBrush.GradientOrigin)" Storyboard.TargetName="BackgroundGradient" d:IsOptimized="True"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundAnimation"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"/>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FocusStates">
              <VisualState x:Name="Focused">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Unfocused"/>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
            <Grid Background="{TemplateBinding Background}" Margin="1">
              <Border x:Name="BackgroundAnimation" Background="#FF448DCA" Opacity="0"/>
              <Rectangle x:Name="BackgroundGradient" Fill="{StaticResource Brush1}">
              </Rectangle>
            </Grid>
          </Border>
          <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
          <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" IsHitTestVisible="false" Opacity="0" RadiusY="3" RadiusX="3"/>
          <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" Opacity="0" RadiusY="2" RadiusX="2" Stroke="#FF6DBDD1" StrokeThickness="1"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文