====== 009 タイマー処理 ====== {{:wiki:windowsphone:7.1:tips:w_phone_009_001.png?200|}} ===== 概要 ===== タイマーによるイメージの移動アニメーションを行います。 ==== MainPage.xaml ==== ==== MainPage.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.Media.Imaging; using System.Windows.Shapes; using System.Windows.Threading; using Microsoft.Phone.Controls; namespace BaseApp { // エントリーポイント public partial class MainPage : PhoneApplicationPage { private const int BASE_POS_X = 480 / 2; private const int BASE_POS_Y = 768 / 2; private Image image0 = null; private double radian = 0.0f; // コンストラクタ public MainPage() { // コンポーネントの初期化。 InitializeComponent(); // 初期化完了後に呼ばれるメソッドの登録。 Loaded += OnLoaded; } // 初期化完了後に呼ばれるメソッド。 void OnLoaded(object sender, RoutedEventArgs args) { // イメージを表示する。 { image0 = new Image(); image0.Source = new BitmapImage(new Uri("sample.png", UriKind.RelativeOrAbsolute)); Canvas.SetLeft(image0, BASE_POS_X - 64 ); Canvas.SetTop(image0, BASE_POS_Y - 64 ); LayoutRoot.Children.Add(image0); } // タイマーの生成と開始。 DispatcherTimer timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 0, 0, 17); timer.Tick += new EventHandler(OnTimerTick); timer.Start(); } // タイマーによる更新処理。 private void OnTimerTick(object sender, EventArgs e) { int px = BASE_POS_X - 64; int py = BASE_POS_Y + (int)(200.0f * Math.Sin(radian)); radian += 0.04f; Canvas.SetLeft(image0, px); Canvas.SetTop(image0, py); } } }