如何在WP8中获取Element的绝对位置,并使用它在新位置移动该元素

时间:2013-10-12 21:11:04

标签: c# windows silverlight windows-phone-8

如何在WP8中获取Element的绝对位置并使用它在新位置移动该元素

我正在尝试使用以下代码在屏幕上使用点按来移动图像并出现意外行为。

        private void SetGame()
    {
        ScreenWidth = Application.Current.Host.Content.ActualWidth;
        ScreenHeight = Application.Current.Host.Content.ActualHeight;

        var transform = littleMan.TransformToVisual(Application.Current.RootVisual);
        System.Windows.Point absolutePosition = transform.Transform(new System.Windows.Point(0, 0));

        manLocY = absolutePosition.Y; //vert
        manLocX = absolutePosition.X; //hori

        leftControlArea = (ScreenWidth / 2);
        rightControlArea = (ScreenWidth / 2); // answer to the screen width;
        topControlArea = (ScreenHeight / 3);
        bottomControlArea = (ScreenHeight / 3) * 2; // answer to the screen height

        littleManWidth = littleMan.ActualWidth;
        littleManHeight = littleMan.ActualHeight;

    }

    private void MoveLittleMan(Vector2 tappedWhere)
    {
        double tapX, tapY;
        tapX = tappedWhere.X; // represent where user tapped on screen width wise
        tapY = tappedWhere.Y; // represent where user tapped on screen height wise


        if(tapY <= topControlArea)
        {
            // move top
            Debug.WriteLine("Move TOP - X: " + tapX + " - Y: " + tapY);
            if (manLocY > littleManHeight)
            {
                manLocY = manLocY - skippingSteps;
                Canvas.SetTop(littleMan, manLocY);
            }
        }

        else if(tapY >= bottomControlArea)
        {
            //move bottom
            Debug.WriteLine("Move BOTTOM - X: " + tapX + " - Y: " + tapY);
            if (manLocY < (ScreenHeight - littleManHeight))
            {
                manLocY = manLocY + skippingSteps;
                Canvas.SetTop(littleMan, manLocY);
            }
        }

        else if (tapX <= leftControlArea && tapY > topControlArea && tapY < bottomControlArea)
        { 
            //move left
            Debug.WriteLine("Move LEFT - X: " + tapX + " - Y: " + tapY);
            if (manLocX > littleManWidth)
            {
                manLocX = manLocX - skippingSteps;
                Canvas.SetLeft(littleMan, manLocX);
            }
        }
        else if (tapX > rightControlArea && tapY > topControlArea && tapY < bottomControlArea)
        { 
            //move right
            Debug.WriteLine("Move RIGHT - X: " + tapX + " - Y: " + tapY);
            if (manLocX < (ScreenWidth - littleManWidth))
            {
                manLocX = manLocX + skippingSteps;
                Canvas.SetLeft(littleMan, manLocX);
            }

        }

    }

预期的行为是,在一个水龙头上,系统会找出屏幕的哪一侧被点击并将小人移动到那个方向。

skippingSteps = 40;

当应用程序启动setGame()函数运行时。我遇到的问题是,当应用程序启动时,我点击屏幕的右侧,小人物跳出屏幕的右边界太远。当我向左按时,它开始回来。使用断点,我收集的数据似乎很好但是当Canvas.SetLeftCanvas.SetTop被称为littleMan时,这是一个20x20的gif图像跳转到X平面中520的位置。

有人可以帮助我理解我做错了什么。

1 个答案:

答案 0 :(得分:0)

好的,所以我通过以下方式解决了我的问题。

我的XAML

    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Canvas Margin="0" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Image Name="littleMan" Source="images/smileyFace.gif" Stretch="None"></Image>
    </Canvas>
    </Grid>

我的代码

    public Game()
    {
        InitializeComponent();
        this.Loaded += Game_Loaded;
    }

    void Game_Loaded(object sender, RoutedEventArgs e)
    {
        SetGame();
        TouchPanel.EnabledGestures = GestureType.Tap;
        LayoutRoot.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(LayoutRoot_ManipulationCompleted);
    }

    void LayoutRoot_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
    {
        while (TouchPanel.IsGestureAvailable)
        {
            GestureSample gesture = TouchPanel.ReadGesture();

            switch (gesture.GestureType)
            {
                case GestureType.Tap:
                    Dispatcher.BeginInvoke(() => MoveLittleMan(gesture.Position));
                    break;
            }
        }
    }

    private void SetGame()
    {
        ScreenWidth = Application.Current.Host.Content.ActualWidth;
        ScreenHeight = Application.Current.Host.Content.ActualHeight;

        // littleman image is in 20 * 20
        littleManWidth = 20;
        littleManHeight = 20; 

        manLocY = (ScreenHeight / 2) + littleManHeight; //vert
        manLocX = (ScreenWidth / 2) + littleManWidth; //hori

        littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);

        leftControlArea = (ScreenWidth / 2);
        rightControlArea = (ScreenWidth / 2); // answer to the screen width;
        topControlArea = (ScreenHeight / 3);
        bottomControlArea = (ScreenHeight / 3) * 2; // answer to the screen height
    }

    private void MoveLittleMan(Vector2 tappedWhere)
    {
        double tapX, tapY;
        tapX = tappedWhere.X; // represent where user tapped on screen width wise
        tapY = tappedWhere.Y; // represent where user tapped on screen height wise


        if(tapY <= topControlArea)
        {
            // move top
            Debug.WriteLine("Move TOP - X: " + tapX + " - Y: " + tapY);
            if (manLocY > littleManHeight)
            {
                manLocY = manLocY - skippingSteps;
                littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
            }
        }

        else if(tapY >= bottomControlArea)
        {
            //move bottom
            Debug.WriteLine("Move BOTTOM - X: " + tapX + " - Y: " + tapY);
            if (manLocY < (ScreenHeight - littleManHeight -50))
            {
                manLocY = manLocY + skippingSteps;
                littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
            }
        }

        else if (tapX <= leftControlArea && tapY > topControlArea && tapY < bottomControlArea)
        { 
            //move left
            Debug.WriteLine("Move LEFT - X: " + tapX + " - Y: " + tapY);
            if (manLocX > littleManWidth)
            {
                manLocX = manLocX - skippingSteps;
                littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
            }
        }
        else if (tapX > rightControlArea && tapY > topControlArea && tapY < bottomControlArea)
        { 
            //move right
            Debug.WriteLine("Move RIGHT - X: " + tapX + " - Y: " + tapY);
            if (manLocX < (ScreenWidth - littleManWidth))
            {
                manLocX = manLocX + skippingSteps;
                littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
            }
        }

    }