调用在cam中宣布的xaml的故事板 - 代码日志

调用在cam中宣布的xaml的故事板

我试图在c#中调用xaml中声明的故事板。

<UserControl.Resources>
    <Storyboard x:Name="PlayStoryboard" x:Key="PlayAnimation">
        ...

我没有从codebehind文件访问“PlayStoryboard”。任何想法我做错了什么?

由于您将Storyboard声明为资源,您可以使用FindResource(“PlayAnimation”)访问它。参见下面的示例:

XAML:

<Window x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:StackOverflow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Storyboard x:Key="PlayAnimation" Storyboard.TargetProperty="(Canvas.Left)">
            <DoubleAnimation From="0" To="100" Duration="0:0:1"/>
        </Storyboard>
    </Window.Resources>

    <Canvas>
        <Button x:Name="btn">Test</Button>
    </Canvas>
</Window>

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Storyboard sb = this.FindResource("PlayAnimation") as Storyboard;
        Storyboard.SetTarget(sb, this.btn);
        sb.Begin();
    }
}
http://stackoverflow.com/questions/3755651/call-a-storyboard-declared-in-xaml-from-c-sharp

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:调用在cam中宣布的xaml的故事板