可以理解成优化版的timer,会阻塞Ui线程,优点是可以自由无规律动画
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using 基于帧动画.Resources;
namespace 基于帧动画
{
public partial class MainPage : PhoneApplicationPage
{
Point mouseLocation;
TranslateTransform tt = new TranslateTransform();
DateTime dt = DateTime.Now;
// 构造函数
public MainPage()
{
InitializeComponent();
CompositionTarget.Rendering+=CompositionTarget_Rendering;
this.rectangle.RenderTransform = tt;
}
private void CompositionTarget_Rendering(object sender, EventArgs e)
{
var currentTime = DateTime.Now;
double elapsedTime = (currentTime - dt).TotalSeconds;
dt = currentTime;
if (mouseLocation != null)
{
tt.X += mouseLocation.X * elapsedTime;
if((tt.X)>330)
{
tt.X = 330;
}
if ((tt.X) <0)
{
tt.X =0;
}
tt.Y += mouseLocation.Y * elapsedTime;
if ((tt.Y) > 450)
{
tt.Y = 450;
}
if ((tt.Y) < 0)
{
tt.Y = 0;
}
}
}
private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
{
mouseLocation = e.GetPosition(this.rectangle);
}
}
}