是否可以全局定义Silverlight 4 Storyboard动画?

时间:2010-11-11 18:28:26

标签: c# silverlight

我的silverlight应用程序中定义了两个动画:

<Storyboard x:Name="ShowControls">
    <DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="SlideOutMenu" />
</Storyboard>
<Storyboard x:Name="Hide-Controls">
    <DoubleAnimation Duration="0:0:0.2" To="180" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="SlideOutMenu" />
</Storyboard> 

当这些故事板在外部样式xaml文件中定义时,它们不起作用。我还没弄清楚为什么我不能以编程方式访问这些资源。

此代码适用于其他资源类型,如画笔和模板,因此......我怀疑此资源字典问题特定于Storyboard类型的资源。

这是抛出错误的代码: (SlideOutMenu是边境控制)。

public void AddEventHandlers()
        {
            SlideOutMenu.MouseEnter += new MouseEventHandler(SlideOutMenu_MouseEnter);
            SlideOutMenu.MouseLeave += new MouseEventHandler(SlideOutMenu_MouseLeave);
        }

        public void SlideOutMenu_MouseEnter(object sender, MouseEventArgs e)
        {

            Storyboard showMenu = Application.Current.Resources["ShowControls"] as Storyboard;
            showMenu.Begin();
        } 

任何想法?

1 个答案:

答案 0 :(得分:2)

我可以发现一些事情

1-故事板Hide-Controls的名称不是有效名称,您需要删除-

2-您正在设置动画的属性不是TranslateX应该是X的正确名称。我假设您使用的是标准TranslateTransform

3-您需要在事件处理程序中连接目标属性。

这是我做的快速测试

<强>的App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="SilverlightApplication1.App"
             >
  <Application.Resources>
    <Storyboard x:Name="ShowControls">
      <DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"  />
    </Storyboard>
    <Storyboard x:Name="HideControls">
      <DoubleAnimation Duration="0:0:0.2" To="180" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.X)"  />
    </Storyboard>
  </Application.Resources>
</Application>

<强> MainPage.xaml中

<UserControl x:Class="SilverlightApplication1.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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">  
  <Canvas x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
    <Border x:Name="SlideOutMenu" BorderBrush="Red" BorderThickness="5">
      <Border.RenderTransform>
        <TranslateTransform X="100" />
      </Border.RenderTransform>
      <ListBox Height="200" Width="100" />
    </Border>
  </Canvas>
</UserControl>

<强> MainPage.xaml.cs中

using System.Windows.Shapes;

namespace SilverlightApplication1
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
      AddEventHandlers();
    }

    public void AddEventHandlers()
    {
      SlideOutMenu.MouseEnter += new MouseEventHandler(SlideOutMenu_MouseEnter);

    }

    public void SlideOutMenu_MouseEnter(object sender, MouseEventArgs e)
    {
      Storyboard showMenu = Application.Current.Resources["ShowControls"] as Storyboard;
      Storyboard.SetTarget(showMenu, SlideOutMenu);
      showMenu.Begin();      
    } 
  }
}