UserControl 加载动画

效果:实现加载UserControl动画效果

cs代码如下

  public  class BaseModuleView : UserControl
    {
        private TranslateTransform CurTranslate;
        private DoubleAnimation XAnim;
        private DoubleAnimation OpacityAnim;
        private double OriginX = 50;

        public BaseModuleView()
        {
            DoInitAnin();
            this.Loaded += BaseModuleView_loaded;
            this.Unloaded += BaseModuleView_Unloaded;
        }

        private void BaseModuleView_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            DoUnloadAnim();
        }
        private void BaseModuleView_loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            DoLoadAnim();
        }
        private void DoLoadAnim()
        {
            this.CurTranslate.BeginAnimation(TranslateTransform.XProperty, XAnim);
            this.BeginAnimation(UIElement.OpacityProperty, OpacityAnim);
        }

        private void DoUnloadAnim()
        {
            this.CurTranslate.BeginAnimation(TranslateTransform.XProperty, null);
            this.BeginAnimation(UIElement.OpacityProperty, null);

            this.CurTranslate.X = this.OriginX;
            this.Opacity = 0;
        }
        private void DoInitAnin()
        {
            CurTranslate = new TranslateTransform();
            CurTranslate.X = OriginX;
            RenderTransform = CurTranslate;
            Opacity = 0;
            XAnim = new DoubleAnimation(0, TimeSpan.FromSeconds(0.3));
            XAnim.EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseInOut };
            OpacityAnim = new DoubleAnimation(1, TimeSpan.FromSeconds(0.2));

        }
    }

XAML代码:

<Modules:BaseModuleView
    x:Class="WFA.OwnUserControlown.EarthDayandnightControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WFA.OwnUserControlown"
             xmlns:Modules="clr-namespace:WFA.Modules"
             mc:Ignorable="d" 
              xmlns:local1="clr-namespace:WFA" d:DesignHeight="540" d:DesignWidth="960" >
    <Grid>
      
    </Grid>
</Modules:BaseModuleView>

 

posted @ 2020-12-30 09:28  可乐_加冰  阅读(392)  评论(0编辑  收藏  举报