在Silverlight中,如何从模板获取动画并通过C#代码使用它?

在Silverlight中,如何从模板获取动画并通过C#代码使用它?,c#,silverlight,.net-4.0,C#,Silverlight,.net 4.0,我有一个控件模板,如下所示: <ControlTemplate x:Key="anchorButton" TargetType="Button"> <Grid x:Name="CommonGrid" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Image x:Name="CommonPic" Source="anchor

我有一个控件模板,如下所示:

<ControlTemplate x:Key="anchorButton" TargetType="Button">
            <Grid x:Name="CommonGrid" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                <Image x:Name="CommonPic" Source="anchor.png" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Opacity="1"/> 
                <Image x:Name="CommonPicSelected" Source="anchorSelected.png" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Opacity="0"/> 
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="FocusStates">
                        <VisualState x:Name="Focused"/>
                        <VisualState x:Name="Unfocused"/>
                    </VisualStateGroup>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal"/>
                        <VisualState x:Name="MouseOver">
                            <Storyboard>
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPic" Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                                </DoubleAnimationUsingKeyFrames>    
                                <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPicSelected" Storyboard.TargetProperty="(UIElement.Opacity)">
                                    <EasingDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                </DoubleAnimationUsingKeyFrames>                            
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Pressed"/>
                        <VisualState x:Name="Disabled"/>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Grid>
        </ControlTemplate>

在应用程序中,我可以在鼠标悬停在按钮上时更改图像,但我需要做的是通过代码进行设置(我正在调用javascript,当我鼠标悬停在行上时,Silverlight应用程序中的相应点应该高亮显示)

指向javascript的链接已完成:

 [ScriptableMember]
 public void UpdateText(int result)
 {
     for (int i = 0; i < 4; i++)
     {
         ButtonBase button = (ButtonBase)VisualTreeHelper.GetChild(RegionCanvas, i);

         if (button.DataContext.ToString().Equals("" + result))
         {
             HtmlPage.Window.Invoke("highlightRow", button.DataContext);
         }
         else
         {
             HtmlPage.Window.Invoke("unHighlightRow", button.DataContext);
         }
    }
}
[ScriptableMember]
公共void UpdateText(int结果)
{
对于(int i=0;i<4;i++)
{
ButtonBase button=(ButtonBase)VisualTreeHelper.GetChild(RegionCanvas,i);
if(button.DataContext.ToString().Equals(“+result))
{
调用(“highlightRow”,button.DataContext);
}
其他的
{
调用(“unHighlightRow”,button.DataContext);
}
}
}

我想在视觉状态下使用动画集,并在上面显示的代码中使用。可能吗?怎么用?如果没有,是否有其他方法使其工作?

您只需在XAML上命名情节提要:

<Storyboard x:Name="storyboard">
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPic" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="CommonPicSelected" Storyboard.TargetProperty="(UIElement.Opacity)">
                            <EasingDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>

如何将其调用到特定对象是我的问题,但我已经通过cs文件直接创建了动画。你尝试过谷歌搜索吗?如果您查看上面的代码,我已经有了可编写脚本的成员。javascript和silverlight之间的链接已经建立。我想要的是链接xaml和cs文件,并在调用脚本成员时使用我在mouseover事件中设置的相同情节提要。我已经用另一种方法解决了。
storyboard.Begin();