c# Login UI with background picture animation

准备4张图片

UI control:

<Grid x:Class="Test1.MainBgAd"
             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" 
             mc:Ignorable="d"  Margin="0" Background="Red"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid.Resources>
        <Storyboard x:Key="sb1">
            <DoubleAnimation From="0" To ="1" Storyboard.TargetName="img" Storyboard.TargetProperty="Opacity" Duration="0:0:4"></DoubleAnimation>
        </Storyboard>
    </Grid.Resources>
    <Grid>  
        <Image Name="imgBg" Stretch="UniformToFill" Opacity="1" Source="/Test1;component/Assets/MianBgAd/minAd2.jpg" />
        <Image Name="img"  Stretch="UniformToFill" Opacity="0" Source="/Test1;component/Assets/MianBgAd/minAd1.jpg" />
      

    </Grid>
</Grid>

  

code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Test1
{
    /// <summary>
    /// Interaction logic for MainBgAd.xaml
    /// </summary>
    public partial class MainBgAd : Grid
    {
        public MainBgAd()
        {
            InitializeComponent();
            loadImags();
            sb=Resources["sb1"] as Storyboard;
            sb.Completed += new EventHandler(sb_Completed);
            Loaded += new RoutedEventHandler(MainBgAd_Loaded);


        }

      
        Storyboard sb;
        int pIndex = 1;
        int MaxIndex = 3;
        List<BitmapImage> imgList = new List<BitmapImage>();
        void MainBgAd_Loaded(object sender, RoutedEventArgs e)
        {
            Start();
        }

         public void Start() {

            sb.Begin();
        
        }


        void sb_Completed(object sender, EventArgs e)
        {

            if (pIndex > MaxIndex)
            {
                pIndex = 0;
            }

            img.Opacity = 0;
        img.Source=imgList[pIndex];
            if (pIndex - 1 < 0)
            {
                imgBg.Source = imgList[MaxIndex];

            }
            else {
                imgBg.Source = imgList[pIndex - 1];
            }
  
            sb.Begin();
             pIndex++;

        }

        void loadImags() {

            for (int i = 1; i <= 4; i++)
            {
                imgList.Add( new BitmapImage(new Uri("/Assets/MianBgAd/minAd"+i+".jpg", UriKind.RelativeOrAbsolute)));

            }
        
        }


    }
}

  

调用:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
gridMain.Children.Insert(0, new MainBgAd());
}

猜你喜欢

转载自www.cnblogs.com/wgscd/p/9188005.html