我应该使用哪种控件类型-Image
,MediaElement
等?
我应该使用哪种控件类型-Image
,MediaElement
等?
class AniImage: Image
{
private Int32Animation ani = new Int32Animation();
private List<Image> images;
public int duration { get; set; }
static FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(new PropertyChangedCallback(ChangedCallbackMethod));
public static readonly DependencyProperty frameProperty = DependencyProperty.Register("Frame", typeof(int), typeof(AniImage), metadata);
static void ChangedCallbackMethod(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
AniImage ai = (AniImage)d;
int curFrame = (int)e.NewValue;
ai.Source = ai.images[curFrame].Source;
}
public int Frame
{
get { return (int)GetValue(frameProperty); }
set { SetValue(frameProperty, value); }
}
public AniImage(List<Image> imgs)
{
images = imgs;
duration = 2;
Frame = 0;
this.Source = imgs[0].Source;
if(images.Count > 1)
{
ani.From = 0;
ani.To = images.Count - 1;
ani.By = 1;
ani.Duration = TimeSpan.FromSeconds(duration);
ani.RepeatBehavior = RepeatBehavior.Forever;
BeginAnimation(frameProperty, ani);
}
}
}
List<Image> imagesList = new List<Image>();
string[] imgNames = {
"Resources/spaceship021.gif",
"Resources/spaceship022.gif",
"Resources/spaceship023.gif",
"Resources/spaceship024.gif",
"Resources/spaceship025.gif" };
foreach (string s in imgNames)
{
Image img = new Image();
img.Source = new BitmapImage(new Uri(s, UriKind.Relative));
imagesList.Add(img);
}
ship = new AniImage(imagesList);
ship.Width = 100;
ship.Height = 100;
Canvas.SetLeft(ship, 250);
Canvas.SetBottom(ship, 50);
field.Children.Add(ship);