c# - WPF 用户控件动画不会工作..?

标签 c# .net wpf xaml

我真的是 WPF 的新手,所以如果你也能给我指一个教程,我会很高兴 :)

这是我的货币代码:

<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>

最佳答案

我完全复制了您的 XAML 并在窗口中运行了它。我所做的唯一更改是用 TextBlock 替换 local:Card 对象(因为我没有 Card 用户控件)。动画完美运行。

因此,要么是您的 local:Card 对象有一些奇怪的东西不允许动画工作,要么是这行中的 Loaded="Card_Loaded" 方法:

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

正在干扰事件触发器:

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

关于c# - WPF 用户控件动画不会工作..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6767164/

相关文章:

c# - API 测试的测试自动化

c# - 来自类库的引用不会复制到正在运行的项目 bin 文件夹

c# - .net 缓存敏感数据是否会以安全方式存在危险或存在任何安全问题?

c# - 我怎样才能得到用户控件wpf的父级

c# - 使用NAudio录制来自麦克风的声音。为什么不能正确记录列表中的整个缓冲区?

c# - 在 vb.net 中隐式实例化的对象?

c# - 如何设置列表属性的默认值?

c# - 绑定(bind) WindowsFormsHost 子属性

c# - MVVM 绑定(bind)问题 - 另一个菜鸟问题

c# - 使用 IPAddress.Address 导致异常