タッチイベントからUIエレメントがタッチされたか判定する

とあるせちがらいAPIの仕様上、どうしてもWindows Phoneのタッチイベントから、アプリケーション内のUIエレメントがタッチされたかを判定する必要があったのでメモ。
※普通は Click イベントつかえばすむ話かと。

MainPage.xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel x:Name="RectangleList" Orientation="Vertical">
            <TextBlock x:Name="StatusText" Height="75" Margin="5" FontSize="35"/>
            <Rectangle x:Name="Rectangle" Fill="Red" Height="300" Width="300"/>
        </StackPanel>
    </Grid>
</Grid>

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
{
    // コンストラクター
    public MainPage()
    {
        InitializeComponent();

        Touch.FrameReported += new TouchFrameEventHandler(OnTouchFrameReported);
    }

    /// <summary>
    /// タッチメッセージ発生後
    /// </summary>
    void OnTouchFrameReported(object sender, TouchFrameEventArgs args)
    {
        //判定対象を取得
        var target = Rectangle;

        //判定対象に対する相対的なタッチ座標値を取得
        var touchPoint = args.GetPrimaryTouchPoint(target);

        //座標が発生したアクションがDownの場合
        if (touchPoint.Action == TouchAction.Down)
        {
            var x = touchPoint.Position.X;
            var y = touchPoint.Position.Y;

            if (x > 0 && y > 0 && x < target.ActualWidth && y < target.ActualHeight)
            {
                StatusText.Foreground = new SolidColorBrush(Colors.Red);
                StatusText.Text = "範囲内がタッチされました";
            }
            else
            {
                StatusText.Foreground = new SolidColorBrush(Colors.White);
                StatusText.Text = "範囲外がタッチされました";
            }
        }
    }
}

実行結果
f:id:chorusde:20111012152207p:image:w240