用户控件内的故事板无法正常工作

时间:2014-10-27 16:36:42

标签: c# windows xaml user-controls

我正在开发Windows 8商店应用程序。我创建了以下用户控件:

<UserControl x:Class="RTV_W8.AnimatedImage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:RTV_W8" 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"> <UserControl.Resources> <!-- Animates the rectangle's opacity. --> <Storyboard x:Name="ContainerGridStoryboard"> <DoubleAnimation Storyboard.TargetName="MainGrid" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:5" AutoReverse="False"> </DoubleAnimation> </Storyboard> </UserControl.Resources> <Grid x:Name="MainGrid" CacheMode="BitmapCache"> <Image Stretch="UniformToFill" x:Name="PictureImage"/> </Grid> </UserControl>

使用以下cs文件(此处仅部分显示)

        public AnimatedImage()
    {
        this.InitializeComponent();
        this.Loaded += AnimatedImage_Loaded;
        this.PictureImage.Loaded += PictureImage_Loaded;
    }

    void AnimatedImage_Loaded(object sender, RoutedEventArgs e)
    {
        this.ContainerGridStoryboard.Begin();
    }

我的问题是在加载图片时故事板没有启动。此用户控件在另一个usercontrol中使用。我尝试了多种变体试图使动画工作,但没有一个工作。我希望用户控件将其不透明度设置为视图,而不仅仅是向上。虽然我可以通过断点看到ContainerGridStoryboard.Begin();在屏幕上没有任何反应

编辑:在另一个用户控件中,后来在datatemplate中使用,如果我在主网格上应用故事板,则用户控件会动画化。但是如果我将它应用于第二个网格(包含在主网格中)或任何其他元素,则动画不起作用。这就是为什么我首先创建了动画图像。

2 个答案:

答案 0 :(得分:1)

我刚刚解决了这个问题。因此,我的用户控件位于网格视图内的数据窗口内的另一个用户控件内的几个网格内。问题是包含动画图像的主网格是用CacheMode =&#34; BitmapCache&#34;设置的。当我删除该属性时,图像逐渐消失。

答案 1 :(得分:0)

当我设置图像的来源,并将UserControl嵌入页面时,图像会按预期淡入。