Here is my code which rotates my object. I have to rotate two same type of objects.
initialization:
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();
}
Code For serial port received data:
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)
{
}
}));
}
I have used here two Dispatcher.BeginInvok' functions because i want to rotate both objects simultaneously. But the issue is that when i use only one object to rotate then no issue come. But when i include second one the first objects does not rotate correctly according to the angle i have mentioned in my program. What i understand that it is a problem that everytime event is fired transformGroup adds a new rotateTransform. So i tried to remove the added TrRot after addition. But this also does not work for me. Thanks.
You can add a simple
Triggerto start your animations in XAML... let's say you add aboolproperty namedIsReady. You could then trigger your animations to start when you set this totrueand to stop when you set it tofalse. Add thisStyleto whichever objects contain theRotateTransformobject that needs to be animated: