移动描画圆落脚位置在C#WPF [英] Moving a drawn circle to settled position in C# WPF

查看:150
本文介绍了移动描画圆落脚位置在C#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击按钮移动的圈子。例;

I would like to move the circle by clicking the buttons. For Example;

有是的(0,0)定位圈,我想通过点击X +按钮来移动它。这将由一个增加X11一个没有停止和动圈上的X位置。然后当我点击Y +按钮,它会增加Ÿ仅20倍和圆开始Y轴移动为好。

There is circle on the position of (0,0) and I would like to move it by clicking on X+ button. This will increase X one by one without stopping and circle move on the X position. Then when I click on the Y+ button, It will increase Y just 20 times and the circle start the move on Y axis as well.

我有一个code,但我不能动态地移动它。它移动到predefined位置。

I have one code, but I couldn't move it dynamically. It moves to predefined position.

XAML:

 <Window x:Class="circle_animation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closed="Window_Closed" >
<Canvas>
    <Ellipse Width="10" Height="10" Canvas.Left="0" Canvas.Top="0" Fill="Black" x:Name="el" />
    <Button Canvas.Left="255" Canvas.Top="266" Content="Move On X" Height="23"  Width="75" Click="Button_Click" />
    <Button Canvas.Left="139" Canvas.Top="272" Content="Move On Y+" Height="23" Name="button1" Width="75" Click="Button2_Click" />
</Canvas>

code:

    public int X;
    public int Y;

    public bool inside = true;


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {


        if (inside)
        {

            DoubleAnimation animatex = new DoubleAnimation();

            animatex.To = X;
            el.BeginAnimation(Canvas.LeftProperty, animatex);

            DoubleAnimation animatey = new DoubleAnimation();
            animatey.To = Y;

            el.BeginAnimation(Canvas.TopProperty, animatey);

        }  
    } 


    public void Button_Click(object sender, RoutedEventArgs e)
    {

        if (inside)
        {
            X++;
        }
    }

    public void Button2_Click(object sender, RoutedEventArgs e)
    {

        Y = Y + 20;
    }

这是我的主意,以动态地移动它,但它不工作。你能帮助我吗?我在哪里做错了?

This was my idea to move it dynamically, but it doesnt work. Can you help me, please ? Where do I do wrong?

推荐答案

您在这里混淆了一些东西。

You are confusing a few things here.

首先,动画不会永远运行,只是因为你没有设置它的持续时间。取而代之的是DoubleAnimation是默认的持续时间为1秒,然后停止。

First, an animation won't run forever just because you do not set its duration. Instead the default duration of a DoubleAnimation is 1 second, then it stops.

二,动画的属性将不会幻化只是因为你改变你分配它事先变量。更改animation`s性能会已经启动后反正忽略。

Second, the animation's To property won't magically change just because you change the variable that you assigned it to beforehand. Changes to the animation`s properties would be ignored anyway after it has been started.

我想使用的动画是错误的方法来解决你的问题。你想达到的(具有可变速度矢量在连续移动的对象)什么也许是最好通过利用的 DispatcherTimer 并根据该经过的时间周期更新所述对象的位置。

I think using animations is the wrong approach to solving your problem. What you want to achieve (a continuously moving object with changeable speed vector) is perhaps best done by utilizing a DispatcherTimer and cyclically updating the object's position according to the elapsed time.

下面的示例code可为您提供有关如何工作的想法。现在,你可以改变但是你喜欢(速度矢量通过设置 speed.X speed.Y 一些按钮点击),以及对象将相应地移动。一种可能的优化是不立即启动定时器,但只有当矢量变为非零,并再次停止时速度变为零。

The following sample code may give you an idea about how this works. You can now change the speed vector however you like (by setting speed.X and speed.Y by some button clicks), and the object will move accordingly. A possible optimization would be not to start the timer immediately, but only when the vector becomes non-zero, and stop it again when speed becomes zero.

private DispatcherTimer timer = new DispatcherTimer(); // timer object
private Vector speed = new Vector(0, 0); // movement in pixels/second, initially zero

public MainWindow()
{
    InitializeComponent();

    timer.Interval = TimeSpan.FromMilliseconds(50); // update 20 times/second
    timer.Tick += TimerTick;
    timer.Start();
}

private void TimerTick(object sender, EventArgs e)
{
    // movement in one interval
    double dx = speed.X * timer.Interval.TotalSeconds;
    double dy = speed.Y * timer.Interval.TotalSeconds;
    // update position
    Canvas.SetLeft(el, Canvas.GetLeft(el) + dx);
    Canvas.SetTop(el, Canvas.GetTop(el) + dy);
}

这篇关于移动描画圆落脚位置在C#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆