locked
图片交替显示代码中抛出异常的问题 RRS feed

  • 问题

  •  

    我有两张图片分别叫0.jpg和1.jpg,想让他们交替显示,code 如下。现象是只能从0.jpg变换到1.jpg. Debug发现的问题是第二次执行time.start时,

    Storyboard sb = new Storyboard();

    sb.Children.Add(animationMap[0]);

    sb.Children.Add(animationMap[1]);

    会抛出异常:Values does not fall within the expected range.

    请教高手这个异常的原因以及解决方法。

    Code Snippet

    Page.xaml:

    <UserControl x:Class="SlidePicSilverlight.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="400" Height="300">

    <Grid x:Name="LayoutRoot" Background="White">

    <Canvas x:Name="myCanvas" Loaded="myCanvas_Loaded"></Canvas>

    </Grid>

    </UserControl>

     

    Page.xaml.cs:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Net;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Animation;

    using System.Windows.Shapes;

    using System.Windows.Media.Imaging;

    using System.Windows.Threading;

    namespace SlidePicSilverlight

    {

    public partial class Page : UserControl

    {

    public Page()

    {

    InitializeComponent();

    }

    private Dictionary<int, Image> imageMap = new Dictionary<int, Image>();

    private Dictionary<int, DoubleAnimation> animationMap = new Dictionary<int, DoubleAnimation>();

    private int CurrentIndex = 0;

    private int NextIndex;

    private DispatcherTimer timer;

    private void myCanvas_Loaded(object sender, RoutedEventArgs e)

    {

    for (int i = 1; i>=0 ; i--)

    {

    Image image = new Image();

    image.Source = new BitmapImage(new Uri(i+".jpg", UriKind.Relative));

    image.Width = 200;

    image.Height = 200;

    myCanvas.Children.Add(image);

    imageMap.Add(i, image);

    }

    DoubleAnimation disapearAnimation = new DoubleAnimation();

    disapearAnimation.Duration = new Duration(TimeSpan.FromSeconds(2.0));

    disapearAnimation.From = 1.0;

    disapearAnimation.To = 0.0;

    DoubleAnimation appearAnimation = new DoubleAnimation();

    appearAnimation.Duration = new Duration(TimeSpan.FromSeconds(2.0));

    appearAnimation.From = 0.0;

    appearAnimation.To = 1.0;

    animationMap.Add(0, disapearAnimation);

    animationMap.Add(1, appearAnimation);

    timer = new DispatcherTimer();

    timer.Interval = TimeSpan.FromSeconds(2.0);

    timer.Tick += new EventHandler(TimeTrick);

    timer.Start();

    }

    private void TimeTrick(object sender, EventArgs e)

    {

    if (CurrentIndex == 1)

    NextIndex = 0;

    else

    NextIndex = CurrentIndex + 1;

    Storyboard.SetTarget(animationMap[0], imageMap[CurrentIndex]);

    Storyboard.SetTargetProperty(animationMap[0], new PropertyPath("(Opacity)"));

    Storyboard.SetTarget(animationMap[1], imageMap[NextIndex]);

    Storyboard.SetTargetProperty(animationMap[1], new PropertyPath("(Opacity)"));

    Storyboard sb = new Storyboard();

    sb.Children.Add(animationMap[0]);

    sb.Children.Add(animationMap[1]);

    myCanvas.Resources.Add("sb", sb);

    sb.Begin();

    CurrentIndex = NextIndex;

    timer.Start();

    }

    }

    }

     

     

    2008年7月31日 12:48

答案

  • 你的代码我修改了一些位置,

    修改如下:

    声明 private

     

    Storyboard sb;为成员变量

    要先关闭sb动画,然后消除动画节点

    if

     

     

    (sb != null

    )

    {

     

    sb.Stop();

    sb.Children.Clear();

    myCanvas.Resources.Remove(

     

    "sb"

    );

    }

    后台源代码::

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;
    using System.Windows.Media.Imaging;
    
    namespace SilverlightApplication3
    {
      public partial class SlidePicSilverlight : UserControl
      {
        public SlidePicSilverlight()
        {
          InitializeComponent();
        }
        private Dictionary<int, Image> imageMap = new Dictionary<int, Image>();
    
        private Dictionary<int, DoubleAnimation> animationMap = new Dictionary<int, DoubleAnimation>();
    
        private int CurrentIndex = 0;
    
        private int NextIndex;
    
        private DispatcherTimer timer;
    
        private void myCanvas_Loaded(object sender, RoutedEventArgs e)
        {
    
          for (int i = 1; i >= 0; i--)
          {
    
            Image image = new Image();
    
            image.Source = new BitmapImage(new Uri(i + ".jpg", UriKind.Relative));
    
            image.Width = 200;
    
            image.Height = 200;
    
            myCanvas.Children.Add(image);
            Canvas.SetLeft(image,i * 100);
            imageMap.Add(i, image);
    
          }
    
          DoubleAnimation disapearAnimation = new DoubleAnimation();
    
          disapearAnimation.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    
          disapearAnimation.From = 1.0;
    
          disapearAnimation.To = 0.0;
    
          DoubleAnimation appearAnimation = new DoubleAnimation();
    
          appearAnimation.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    
          appearAnimation.From = 0.0;
    
          appearAnimation.To = 1.0;
    
          animationMap.Add(0, disapearAnimation);
    
          animationMap.Add(1, appearAnimation);
    
    
          timer = new DispatcherTimer();
    
          timer.Interval = TimeSpan.FromSeconds(2.0);
    
          timer.Tick += new EventHandler(TimeTrick);
    
          timer.Start();
    
        }
    
        private Storyboard sb;
        private void TimeTrick(object sender, EventArgs e)
        {
    
          if (sb != null)
          {
            
            sb.Stop();
            sb.Children.Clear();
            myCanvas.Resources.Remove("sb");
          }
          if (CurrentIndex == 1)
    
            NextIndex = 0;
    
          else
    
            NextIndex = CurrentIndex + 1;
    
          Storyboard.SetTarget(animationMap[0], imageMap[CurrentIndex]);
    
          Storyboard.SetTargetProperty(animationMap[0], new PropertyPath("(Opacity)"));
    
          Storyboard.SetTarget(animationMap[1], imageMap[NextIndex]);
    
          Storyboard.SetTargetProperty(animationMap[1], new PropertyPath("(Opacity)"));
    
    
          sb = new Storyboard();
    
          sb.Children.Add(animationMap[0]);
    
          sb.Children.Add(animationMap[1]);
    
          myCanvas.Resources.Add("sb", sb);
    
          sb.Begin();
    
          CurrentIndex = NextIndex;
    
          timer.Start();
    
        }
    
      }// end for class
    }
    
    

     


    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    • 已标记为答案 天使坠 2011年7月12日 5:47
    2011年7月12日 5:43

全部回复

  • 你的代码我修改了一些位置,

    修改如下:

    声明 private

     

    Storyboard sb;为成员变量

    要先关闭sb动画,然后消除动画节点

    if

     

     

    (sb != null

    )

    {

     

    sb.Stop();

    sb.Children.Clear();

    myCanvas.Resources.Remove(

     

    "sb"

    );

    }

    后台源代码::

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;
    using System.Windows.Media.Imaging;
    
    namespace SilverlightApplication3
    {
      public partial class SlidePicSilverlight : UserControl
      {
        public SlidePicSilverlight()
        {
          InitializeComponent();
        }
        private Dictionary<int, Image> imageMap = new Dictionary<int, Image>();
    
        private Dictionary<int, DoubleAnimation> animationMap = new Dictionary<int, DoubleAnimation>();
    
        private int CurrentIndex = 0;
    
        private int NextIndex;
    
        private DispatcherTimer timer;
    
        private void myCanvas_Loaded(object sender, RoutedEventArgs e)
        {
    
          for (int i = 1; i >= 0; i--)
          {
    
            Image image = new Image();
    
            image.Source = new BitmapImage(new Uri(i + ".jpg", UriKind.Relative));
    
            image.Width = 200;
    
            image.Height = 200;
    
            myCanvas.Children.Add(image);
            Canvas.SetLeft(image,i * 100);
            imageMap.Add(i, image);
    
          }
    
          DoubleAnimation disapearAnimation = new DoubleAnimation();
    
          disapearAnimation.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    
          disapearAnimation.From = 1.0;
    
          disapearAnimation.To = 0.0;
    
          DoubleAnimation appearAnimation = new DoubleAnimation();
    
          appearAnimation.Duration = new Duration(TimeSpan.FromSeconds(2.0));
    
          appearAnimation.From = 0.0;
    
          appearAnimation.To = 1.0;
    
          animationMap.Add(0, disapearAnimation);
    
          animationMap.Add(1, appearAnimation);
    
    
          timer = new DispatcherTimer();
    
          timer.Interval = TimeSpan.FromSeconds(2.0);
    
          timer.Tick += new EventHandler(TimeTrick);
    
          timer.Start();
    
        }
    
        private Storyboard sb;
        private void TimeTrick(object sender, EventArgs e)
        {
    
          if (sb != null)
          {
            
            sb.Stop();
            sb.Children.Clear();
            myCanvas.Resources.Remove("sb");
          }
          if (CurrentIndex == 1)
    
            NextIndex = 0;
    
          else
    
            NextIndex = CurrentIndex + 1;
    
          Storyboard.SetTarget(animationMap[0], imageMap[CurrentIndex]);
    
          Storyboard.SetTargetProperty(animationMap[0], new PropertyPath("(Opacity)"));
    
          Storyboard.SetTarget(animationMap[1], imageMap[NextIndex]);
    
          Storyboard.SetTargetProperty(animationMap[1], new PropertyPath("(Opacity)"));
    
    
          sb = new Storyboard();
    
          sb.Children.Add(animationMap[0]);
    
          sb.Children.Add(animationMap[1]);
    
          myCanvas.Resources.Add("sb", sb);
    
          sb.Begin();
    
          CurrentIndex = NextIndex;
    
          timer.Start();
    
        }
    
      }// end for class
    }
    
    

     


    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    • 已标记为答案 天使坠 2011年7月12日 5:47
    2011年7月12日 5:43
  • Silverlight4以后,你可以直接用blend做一个动画出来,没有必要写的这么复杂。

    如果你需要动态的根据列表来一张一张的加载动画,建议你也可以使用帧动画


    努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!
    2011年7月12日 5:47