Simulate a propeller

时间:2016-03-02 11:01:32

标签: c# wpf xaml canvas

I have this code that display a rectangle on a canvas :

XAML :

<Window x:Class="rotateRect.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:rotateRect"
    mc:Ignorable="d"
    Title="MainWindow" Height="402.027" Width="600.676">
<Grid>

    <Canvas Name="my_c" HorizontalAlignment="Left" Height="24" Margin="160,149,0,0"  VerticalAlignment="Top" Width="177" RenderTransformOrigin="0.5,0.5">
        <Canvas.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="-68.962"/>
                <TranslateTransform/>
            </TransformGroup>
        </Canvas.RenderTransform>
    </Canvas>

</Grid>

I can turn it in XAML with "RenderTransform". But i wanted to implement this rotation in c# and create a "propeller". I tried to find information about "RotateTransform" for a canvas. Thys is my c# code :

C# :

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace rotateRect
{
    public partial class MainWindow : Window
    {
        Rectangle my_r = new Rectangle();

        public MainWindow()
        {
            InitializeComponent();
            initializeRect(my_r, my_c);
        }

        private void initializeRect(Rectangle r, Canvas c)
        {
            // rectangle 
            r.Width = 10;
            r.Height = 50;

            // rectangle Color
            r.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 255));

            // canvas 
            c.Children.Add(r);
            c.Width = 10;
            c.Height = 50;
        }
    }
}

I found an exemple of "RotateTransform" for a bitmap object but i hadn't find anything for a canvas. C#, rotating Graphics?

It's possible to apply a RotateTransform to a canvas in c#?

2 个答案:

答案 0 :(得分:0)

修改initializeRect方法

private void initializeRect(Rectangle r, Canvas c)
        {
            // rectangle 
            r.Width = 10;
            r.Height = 50;

            // rectangle Color
            r.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 255));

            // canvas 
            c.Children.Add(r);
            c.Width = 10;
            c.Height = 50;

            RotateTransform rt = new RotateTransform();
            rt.Angle=-68.962;
            c.RenderTransform = rt;
        }

答案 1 :(得分:0)

'螺旋桨'你的意思是动画吗?为此,你需要动画。只有RotateTransform是不够的。

例如,让我们使用Rectangle复制你的Canvas但是在XAML中:

<Canvas Name="cnvs" VerticalAlignment="Center" HorizontalAlignment="Center"
        Loaded="cnvs_Loaded">
    <Canvas.Resources>
        <DoubleAnimation x:Key="rotator" From="0" To="360" Duration="0:0:1"
                         AutoReverse="False" RepeatBehavior="Forever" />
    </Canvas.Resources>
    <Canvas.RenderTransform>
        <RotateTransform x:Name="canvasRotation" />
    </Canvas.RenderTransform>

    <Rectangle Width="10" Height="50" Fill="Blue" />
</Canvas>

在我们的Loaded事件处理程序中启动动画。 Canvas有它自己的尺寸怪癖,所以我们需要为变换设置CenterX和CenterY的特性:

private void cnvs_Loaded(object sender, RoutedEventArgs e)
{
     var canvas = (Canvas) sender;
     var animation = canvas.Resources["rotator"] as DoubleAnimation;

     var children = canvas.Children.OfType<FrameworkElement>().ToArray();
     canvasRotation.CenterX = children.Max(c => c.ActualWidth) / 2;
     canvasRotation.CenterY = children.Max(c => c.ActualHeight) / 2;

     canvasRotation.BeginAnimation(RotateTransform.AngleProperty, animation);
 }

<小时/> 但你真的需要一个Canvas吗?我们可以使用Grid简化事情,例如:

<Grid Name="grid" VerticalAlignment="Top" HorizontalAlignment="Left"
        Loaded="grid_Loaded" RenderTransformOrigin="0.5,0.5">
    <Grid.Resources>
        <DoubleAnimation x:Key="rotator" From="0" To="360" Duration="0:0:1"
                         AutoReverse="False" RepeatBehavior="Forever" />
    </Grid.Resources>
    <Grid.RenderTransform>
        <RotateTransform x:Name="gridRotation" />
    </Grid.RenderTransform>

    <Rectangle Width="10" Height="50" Fill="Blue" />
</Grid>

private void grid_Loaded(object sender, RoutedEventArgs e)
{
     var g = (Grid) sender;
     var animation = g.Resources["rotator"] as DoubleAnimation;        
     gridRotation.BeginAnimation(RotateTransform.AngleProperty, animation);
}

或者只为Recntagle设置动画,如果它是容器中唯一的对象。无需复杂化。