转换无法正常工作

时间:2013-09-04 10:50:45

标签: wpf transformation rotatetransform transformgroup

这是我的代码,它旋转我的对象。我必须旋转两种相同类型的对象。

  

初​​始化:

private TransformGroup trGrp;
        private RotateTransform trRot;
        private TransformGroup trGrp1;
        private RotateTransform trRot1;
        static private double angle;
        SerialPort port;
        value v;
        string result;
        public int zeroValue;
        public MainWindow()
        {
            InitializeComponent();
            angle = 0;
            zeroValue = 327;
            trGrp = new TransformGroup();
            trRot = new RotateTransform(angle) { CenterX = rectangle1.Width / 2, CenterY = rectangle1.Height };
            trGrp1 = new TransformGroup();
            trRot1 = new RotateTransform(angle) { CenterX = rectangle1.Width / 2, CenterY = rectangle1.Height };
           // trGrp.Children.Add(trRot);
            //trGrp1.Children.Add(trRot1);
            v = new value();
            port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
        }
  

代码用于串口接收数据:

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!port.IsOpen)
            { 
                port.Open(); 
            }

            try
            {
                result = port.ReadLine();
                result = result.Replace("\r", "");
            }

            catch (Exception e1)
            { MessageBox.Show(e1.Message); }
            int[] temp = new int[4];
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background,
        new Action(
          delegate()
          {


              try
              {

                      temp[0] = int.Parse(result[1].ToString() + result[2].ToString() + result[3].ToString() + result[4].ToString());

                      if (temp[0] == zeroValue)
                      {

                          trRot.Angle = 0;
                          trGrp.Children.Add(trRot);
                          rectangle1.RenderTransform = trGrp;
                          trGrp.Children.RemoveAt(0);//because Add function adds a new trGrp so its size increases and we do not need old trGrp so we remove them.
                          label3.Content = trRot.Angle.ToString();

                      }

                      else if (temp[0] > zeroValue)
                      {

                          int totalValues = temp[0] - zeroValue;
                          if (totalValues == 1) 
                          {
                              totalValues = 0;
                          }
                          textBox3.Text = temp[0].ToString();
                          angle = totalValues * Properties.Settings.Default.angle;

                          if (angle<31)
                          {
                              trRot.Angle = angle / 2;
                              trGrp.Children.Add(trRot);
                              rectangle1.RenderTransform = trGrp;
                              //trGrp.Children.RemoveAt(1);//because Add function adds a new trGrp so its size increases and we do not need old trGrp so we remove them.
                              trGrp.Children.Remove(trRot);
                              label3.Content = angle.ToString();
                          }




                      }
                      else if (temp[0] < zeroValue)
                      {

                          int totalValues = zeroValue - temp[0];
                          if (totalValues==1)
                          {
                              totalValues = 0;
                          }
                          textBox3.Text = temp[0].ToString();
                          angle = -totalValues * Properties.Settings.Default.angle;

                          if (angle> -21)
                          {
                              trRot.Angle = angle / 2;
                          trGrp.Children.Add(trRot);
                          rectangle1.RenderTransform = trGrp;
                          trGrp.Children.Remove(trRot);//because Add function adds a new trGrp so its size increases and we do not need old trGrp so we remove them.
                          label3.Content = angle.ToString();
                          }




                      }

              }
              catch (Exception)
              { }
          }
      ));
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background,
        new Action(
          delegate()
          {

              try
              {
                  temp[1] = int.Parse(result[6].ToString() + result[7].ToString() + result[8].ToString() + result[9].ToString());
                  if (temp[1] == zeroValue)
                  {

                      trRot1.Angle = 0;
                      trGrp.Children.Add(trRot1);
                      rectangle2.RenderTransform = trGrp1;
                      trGrp1.Children.RemoveAt(1);//because Add function adds a new trGrp so its size increases and we do not need old trGrp so we remove them.
                      // textBox3.Text = trRot.Angle.ToString();
                      label4.Content = angle.ToString();

                  }

                  else if (temp[1] > zeroValue)
                  {

                      int totalValues = temp[1] - zeroValue;
                      if (totalValues == 1)
                      {
                          totalValues = 0;
                      }
                      angle = totalValues * Properties.Settings.Default.angle;
                      angle = Math.Round(angle, 0);
                      if (angle < 21)
                      {
                          trRot1.Angle = angle / 2;
                          trGrp1.Children.Add(trRot1);
                          rectangle2.RenderTransform = trGrp1;
                          trGrp1.Children.RemoveAt(0);//because Add function adds a new trGrp so its size increases and we do not need old trGrp so we remove them.
                          label4.Content = angle.ToString();
                      }
                  }
                  else if (temp[1] < zeroValue)
                  {

                      int totalValues = zeroValue - temp[1];
                      if (totalValues == 1)
                      {
                          totalValues = 0;
                      }
                      angle = -totalValues * Properties.Settings.Default.angle;

                      if (angle > -21)
                      {
                          trRot1.Angle = angle / 2;
                          trGrp1.Children.Add(trRot1);
                          rectangle2.RenderTransform = trGrp1;
                          trGrp1.Children.RemoveAt(0);//because Add function adds a new trGrp so its size increases and we do not need old trGrp so we remove them.
                          label4.Content = angle.ToString();
                      }


                  }
              }
              catch (Exception)
              {


              }
          }));


        }

我在这里使用了两个Dispatcher.BeginInvok'函数,因为我想同时旋转两个对象。但问题是,当我只使用一个物体旋转时,没有问题。但是当我包含第二个对象时,根据我在程序中提到的角度,第一个对象无法正确旋转。据我所知,每次事件被触发都是一个问题,transformGroup添加了一个新的rotateTransform。所以我试图在添加后删除添加的TrRot。但这对我也不起作用。 谢谢。

1 个答案:

答案 0 :(得分:0)

您可以添加一个简单的Trigger来启动XAML中的动画...假设您添加了一个名为bool的{​​{1}}属性。然后,当您将其设置为IsReady时,可以触发动画启动,并在将其设置为true时停止。将此false添加到包含需要设置动画的Style对象的对象中:

RotateTransform