WPF UserControl动画不会工作..?

时间:2011-07-20 19:07:27

标签: c# .net wpf xaml

我是WPF的新手,所以如果你能指点我一个tutoiral我会非常高兴:)

这是我的货代码:

<Grid Name="Grid">
    <local:Card Loaded="Card_Loaded"
                x:Name="MyCard">
        <local:Card.Triggers>
            <EventTrigger RoutedEvent="local:Card.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="MyCard"
                                         Storyboard.TargetProperty="Opacity"
                                         From="1.0"
                                         To="0.0"
                                         Duration="0:0:5"
                                         AutoReverse="True"
                                         RepeatBehavior="Forever" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </local:Card.Triggers>
    </local:Card>
</Grid>

local:Card是UserControl

这就是为什么我使用 x:Name =“”而不是 Name =“”

Because 'MS.Internal.Design.Metadata.ReflectionTypeNode' is implemented in the same assembly, you must set the x:Name attribute rather than the MS.Internal.Design.Metadata.ReflectionPropertyNode attribute.

我可以看到卡片,除了动画之外的所有东西都不起作用= \

这是卡片XAML:

<UserControl.Resources>
    <x:Array Type="{x:Type s:String}"
             x:Key="src">
        <s:String>Foo</s:String>
    </x:Array>
    <DataTemplate x:Key="frontTemplate">
        <Grid Background="Transparent">
            <Image Source="Images\Card.jpg" />
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="backTemplate">
        <GroupBox Header="Back"
                  Background="White">
            <StackPanel>
                <RadioButton Content="This"
                             IsChecked="True" />
                <RadioButton Content="Is" />
                <RadioButton Content="The" />
                <RadioButton Content="Back" />
            </StackPanel>
        </GroupBox>
    </DataTemplate>
</UserControl.Resources>
<ScrollViewer>
    <ItemsControl Width="180"
                  Height="250"
                  ItemsSource="{StaticResource src}"
                  ItemTemplate="{StaticResource flipItemTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</ScrollViewer>

3 个答案:

答案 0 :(得分:2)

我完全复制了你的XAML并在一个窗口中运行它。我做的唯一改变是用TextBlock替换local:Card对象(因为我没有Card usercontrol)。动画完美无缺。

因此,您的local:Card对象有一些不允许动画工作的奇怪内容或此行中的Loaded="Card_Loaded"方法:

<local:Card Loaded="Card_Loaded" x:Name="MyCard">

正在干扰事件触发器:

<EventTrigger RoutedEvent="local:Card.Loaded">

答案 1 :(得分:0)

将故事板放在EventTrigger.EnterActions或EventTrigger.Actions标记中。

我最近潜入了一些WPF动画......这是让我开始的链接: http://www.galasoft.ch/mydotnet/articles/article-2006102701.aspx

答案 2 :(得分:0)

解决方案:

<Grid Name="Grid">
    <local:Card x:Name="MyCard" MouseEnter="MyCard_MouseEnter" />
</Grid>
<Window.Resources>
    <Storyboard x:Key="sbdCardAnim">
        <DoubleAnimation
            Storyboard.TargetName="MyCard" 
            Storyboard.TargetProperty="Opacity"
            From="1" To="0" Duration="0:0:5"
            AutoReverse="True" RepeatBehavior="Forever" />
    </Storyboard>
</Window.Resources>

C#:

    private void MyCard_MouseEnter(object sender, MouseEventArgs e)
    {
        Storyboard sbdCardAnim = (Storyboard)FindResource("sbdCardAnim");
        sbdCardAnim.Begin(this);
    }
相关问题