gpt4 book ai didi

wpf - 在样式的名称范围中找不到 Storyboard

转载 作者:行者123 更新时间:2023-12-03 19:01:17 25 4
gpt4 key购买 nike

我遇到的问题是,如果我基于包含 Storyboard 的第二个样式创建样式,则 Trigger.ExitAction 中会出现异常。

  • 如果我将鼠标悬停在下面演示中的任何一个矩形上, Storyboard就会运行并且矩形会改变颜色。
  • 当鼠标离开带有 style='rectStyle' 的红色矩形时, Storyboard被移除。
  • 当鼠标离开蓝色矩形(使用派生样式)时,我收到此异常:

    无效操作异常:
    在“System.Windows.Style”的名称范围内找不到“MouseOverStoryboard”名称。

  • 所以:
  • 具有基本样式的 Storyboard是否有效?
  • 是否有更明确的方式来引用 BeginStoryboardName 以便不会发生此错误?
  • 还有其他建议吗?

  • 最后,我试图实现重用包含其他几种样式的触发器和 Storyboard的样式。

    下面是一些演示问题的简单代码:
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid.Resources>
    <Style x:Key="rectStyle" TargetType="{x:Type Rectangle}">
    <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Trigger.EnterActions>
    <BeginStoryboard x:Name="MouseOverStoryboard">
    <Storyboard>
    <ColorAnimation To="PaleGoldenrod"
    Storyboard.TargetProperty="(Fill).(Color)"
    Duration="0:0:1"/>
    </Storyboard>
    </BeginStoryboard>
    </Trigger.EnterActions>
    <Trigger.ExitActions>
    <StopStoryboard BeginStoryboardName="MouseOverStoryboard"/>
    </Trigger.ExitActions>
    </Trigger>
    </Style.Triggers>
    </Style>

    <Style x:Key="rectStyle2" TargetType="{x:Type Rectangle}"
    BasedOn="{StaticResource rectStyle}"/>
    </Grid.Resources>

    <Rectangle Grid.Row="0" Width="100" Height="100" Fill="Red"
    Style="{StaticResource rectStyle}" />
    <Rectangle Grid.Row="1" Width="100" Height="100" Fill="Blue"
    Style="{StaticResource rectStyle2}" />
    </Grid>

    最佳答案

    如果您想包含 StopStoryboard,最好的办法是避开派生样式。 .该样式实际上有 2 个实例,并且派生样式的范围是错误的。我相信这是设计使然。您可以将该 Storyboard抽象到资源中的外部范围,并以两种样式复制触发器,通过 StaticResource 引用 Storyboard。 .它可能无法解决您的问题,因为您可能对派生样式有特定要求,但不幸的是,我认为您没有选择。

    因为如果您想使用 StopStoryboard,您根本无法从基本样式继承。你必须做这样的事情:

    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.Resources>
    <Storyboard x:Key="mouseOver">
    <ColorAnimation
    AutoReverse="True"
    Duration="0:0:1"
    RepeatBehavior="Forever"
    Storyboard.TargetProperty="(Fill).(Color)"
    To="PaleGoldenrod"/>
    </Storyboard>
    <Style x:Key="rectStyle" TargetType="{x:Type Rectangle}">
    <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Trigger.EnterActions>
    <BeginStoryboard x:Name="MouseOverStoryboard" Storyboard="{StaticResource mouseOver}"/>
    </Trigger.EnterActions>
    <Trigger.ExitActions>
    <StopStoryboard BeginStoryboardName="MouseOverStoryboard"/>
    </Trigger.ExitActions>
    </Trigger>
    </Style.Triggers>
    </Style>
    <Style x:Key="rectStyle2" TargetType="{x:Type Rectangle}">
    <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Trigger.EnterActions>
    <BeginStoryboard x:Name="MouseOverStoryboard1" Storyboard="{StaticResource mouseOver}"/>
    </Trigger.EnterActions>
    <Trigger.ExitActions>
    <StopStoryboard BeginStoryboardName="MouseOverStoryboard1"/>
    </Trigger.ExitActions>
    </Trigger>
    </Style.Triggers>
    </Style>
    </Grid.Resources>
    <Rectangle
    Width="100"
    Height="100"
    Grid.Row="0"
    Fill="Red"
    Style="{StaticResource rectStyle}"/>
    <Rectangle
    Width="100"
    Height="100"
    Grid.Row="1"
    Fill="Blue"
    Style="{StaticResource rectStyle2}"/>

    关于wpf - 在样式的名称范围中找不到 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9920803/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com