Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 网格背景图像不透明度动画_Wpf_Animation - Fatal编程技术网

Wpf 网格背景图像不透明度动画

Wpf 网格背景图像不透明度动画,wpf,animation,Wpf,Animation,使用wpf创建了一个简单的动画,希望在幻灯片之间添加动画,使背景淡入淡出,更改背景图像,最后在新的背景图像中淡入淡出。我试着做下面的事情…我没有收到任何错误,背景改变,但也没有任何动画…我做错了什么 public void SetSlider(MyItem item) { //Fade out DoubleAnimation fadeOutAnimation = new DoubleAnimation(0, TimeSpan.FromSeconds(3))

使用wpf创建了一个简单的动画,希望在幻灯片之间添加动画,使背景淡入淡出,更改背景图像,最后在新的背景图像中淡入淡出。我试着做下面的事情…我没有收到任何错误,背景改变,但也没有任何动画…我做错了什么

public void SetSlider(MyItem item)
    {
        //Fade out
        DoubleAnimation fadeOutAnimation = new DoubleAnimation(0, TimeSpan.FromSeconds(3));
        fadeOutAnimation.AutoReverse = false;
        grdContent.Background.BeginAnimation(Brush.OpacityProperty, fadeOutAnimation);

        //set background
        ImageBrush bgBrush = new ImageBrush();
        bgBrush.ImageSource = new BitmapImage(new Uri(item.ImageFile.SavedDirectoryAndFile, UriKind.Absolute));
        grdContent.Background = bgBrush;

        //Set title
        txtTitle.Text = item.Title;

        //set Summary
        txtSummary.Text = item.Summary;

        //Fade back in
        DoubleAnimation fadeInAnimation = new DoubleAnimation(1, TimeSpan.FromSeconds(3));
        fadeInAnimation.AutoReverse = false;
        grdContent.Background.BeginAnimation(Brush.OpacityProperty, fadeInAnimation);        
} 

一个更简单的方法是使用一个图像动画的示例项目

参考资料:

xmlns:trans="clr-namespace:Transitionals;assembly=Transitionals"
xmlns:transc="clr-namespace:Transitionals.Controls;assembly=Transitionals"
xmlns:transt="clr-namespace:Transitionals.Transitions;assembly=Transitionals"
xmlns:refl="clr-namespace:System.Reflection;assembly=mscorlib"


 <transc:TransitionElement x:Name="TransitionBox" Grid.Row="1" Content="{Binding 
             CurrentImage}">
   <transc:TransitionElement.Transition>
      <transt:TranslateTransition StartPoint="1,0" EndPoint="0,0"  
                Duration="0:0:0.6"/>
        </transc:TransitionElement.Transition>
    </transc:TransitionElement>
xmlns:trans=“clr命名空间:Transitionals;assembly=Transitionals”
xmlns:transc=“clr命名空间:Transitionals.Controls;assembly=Transitionals”
xmlns:transt=“clr命名空间:Transitionals.Transitions;assembly=Transitionals”
xmlns:refl=“clr命名空间:System.Reflection;assembly=mscorlib”
或者使用幻灯片控制,如果它是一个自动转换

 <transc:Slideshow.TransitionSelector>
                <trans:RandomTransitionSelector>
                    <trans:RandomTransitionSelector.TransitionAssemblies>
                        <refl:AssemblyName Name="Transitionals" />
                    </trans:RandomTransitionSelector.TransitionAssemblies>
                </trans:RandomTransitionSelector>
 </transc:Slideshow.TransitionSelector>

找到了答案。。。我必须将动画应用到背景属性中设置的笔刷上…而不是背景属性本身,我必须进行一些计时更改。以下是我的最终解决方案:

public void SetSlider(MyItem item)
    {
        //Create the fade out animation. 
        DoubleAnimation fadeOutAnimation = new DoubleAnimation(0, TimeSpan.FromMilliseconds(500));
        fadeOutAnimation.AutoReverse = false;

        //wait until the first animation is complete before changing the background, or else it will appear to just "fadeIn" with now fadeout.
        fadeOutAnimation.Completed += delegate(object sender, EventArgs e)
        {
            //once the fadeout is complete set the new back ground and fade back in. 
            //Create a new background brush. 
            ImageBrush bgBrush = new ImageBrush();
            bgBrush.ImageSource = new BitmapImage(new Uri(item.ImageFile.SavedDirectoryAndFile, UriKind.Absolute));
            bgBrush.Opacity = 0;

            //Set the grid background to the new brush. 
            grdContent.Background = bgBrush; 

            //Set the brush...(not the background property) with the animation. 
            DoubleAnimation fadeInAnimation = new DoubleAnimation(1, TimeSpan.FromMilliseconds(500));
            fadeInAnimation.AutoReverse = false;
            bgBrush.BeginAnimation(Brush.OpacityProperty, fadeInAnimation);
        };

        //Fade out..before changing the background. 
        var currentBackground = grdContent.Background;
        currentBackground.BeginAnimation(Brush.OpacityProperty, fadeOutAnimation);

        //Set title
        txtTitle.Text = item.Title;

        //set Summary
        txtSummary.Text = item.Summary;
    }

到目前为止,我找到了最好的解决方案,谢谢;)