【问题标题】:Bind Silverlight Animation to Button Click将 Silverlight 动画绑定到按钮单击
【发布时间】:2010-10-25 19:40:09
【问题描述】:

我正在尝试通过单击按钮来启动动画。以下代码看起来很合理,但会引发运行时错误。理想情况下,我只想将情节提要指向命令。这可能吗(我用谷歌搜索并没有发现任何迹象表明它是)

                <Button Width="50" Height="24" Content="X">
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <BeginStoryboard Storyboard="{StaticResource ShowTemplateSelector}">
                            </BeginStoryboard>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>

这是故事板:

<UserControl.Resources>
    <Storyboard x:Name="ShowTemplateSelector">
        <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Width" From="0" To="332" Duration="0:0:0.4" />
        <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Height" From="0" To="100" Duration="0:0:0.4" />
    </Storyboard>

运行时错误提示:

Microsoft JScript 运行时错误:Silverlight 应用程序中出现未处理错误 2531 发生错误。 [行:97 位置:55] 在 System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator) 在 SilverlightApplication8.MainPage.InitializeComponent() 在 SilverlightApplication8.MainPage..ctor() 在 SilverlightApplication8.App.Application_Startup(对象发送者,StartupEventArgs e) 在 MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,委托 handlerDelegate,对象发送者,对象参数) 在 MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

第 97 行是这样的:

<EventTrigger RoutedEvent="Button.Click">

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    您可以使用 Blend SDK 获得您想要的东西。只需将 ControlStoryboardAction 行为附加到您的按钮:

        <Button Width="50" Height="24" Content="X">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <ei:ControlStoryboardAction Storyboard="{StaticResource ShowTemplateSelector}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    

    其中 i 和 ei 定义如下:

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    

    【讨论】:

      【解决方案2】:

      您需要将情节提要移动到触发器中。如果您尝试将其作为 StaticResource 引用,则无法解析 ResourceDictionary 命名范围内的名称

      <EventTrigger RoutedEvent="Button.Click">
        <BeginStoryboard>
          <BeginStoryboard.Storyboard>
            <Storyboard x:Name="ShowTemplateSelector">
              <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Width" From="0" To="332" Duration="0:0:0.4" />
              <DoubleAnimation Storyboard.TargetName="canvasStyleSelected" Storyboard.TargetProperty="Height" From="0" To="100" Duration="0:0:0.4" />
            </Storyboard>
        </BeginStoryboard.Storyboard>
      </BeginStoryboard>
      

      【讨论】:

      • 谢谢。是否有 any 方法可以让按钮引用资源部分中的情节提要?我的意思是,除了 ShowTemplateSelector.Begin();当然是在点击处理程序中。
      • 另外,这是 Silverlight 的限制,还是 WPF 也会导致同样的错误?
      • 我的解释有点措辞不当。问题不在于找到故事板。 StaticResource 标记扩展将使情节提要很好。问题是 Storyboard.TargetName="canvasStyleSelected" 无法解析名称 canvasStyleSelected 因为它不在范围内。 WPF 中的 DynamicResource 会起作用,因为它会从执行范围内查找名称,但不幸的是,SL 中不存在该扩展名。这是我发现做你所描述的唯一方法。
      • 使用此方法会为我抛出 failed to assign to property 'system.windows.eventtrigger.routedevent' 错误
      【解决方案3】:

      改变这个...

      <BeginStoryboard Storyboard="{StaticResource ShowTemplateSelector}">
      </BeginStoryboard>
      

      到这个...

      <BeginStoryboard Storyboard="{DynamicResource ShowTemplateSelector}">
      </BeginStoryboard>
      

      【讨论】:

      • DynamicResource 标记扩展在 Silverlight 中不存在。它仅在 WPF 中。
      • @Stephan 感谢您提供的信息...只是查了一下,它说如果您要从 WPF 转移到 SL 以转移到 StaticResource 并以这种方式引用它...这就是他正在做……嗯……
      • 相信我,我希望这是解决方案。我真的希望 MS 能在 SL 4 中添加这个。
      • SL 4 中似乎还有很多内容没有添加......“它在 SL 中不起作用”似乎是一个共同的主题:(
      • @Adam 在 SL 2 中编写了一个 LOB 应用程序...相信我 SL 4 相比之下看起来很棒...
      猜你喜欢
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 2013-12-25
      相关资源
      最近更新 更多