我想制作影片,畫布上的一個矩形向上。這應該發生在后面的編碼器中。最后,我想讓矩形在while回圈中上下移動,但我什至不能讓它朝一個方向移動。
xml:
<Window x:Class="WpfApp.View.CPR"
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:WpfApp.View"
mc:Ignorable="d"
Title="CPR" Height="250" Width="210">
<Grid Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1">
<TextBlock Text="Counter" HorizontalAlignment="Center" Grid.Column="1" Grid.Row="0" Background="Black" Foreground="White"/>
<TextBlock Text="" FontSize="20" FontWeight="Bold" HorizontalAlignment="Center" Grid.Column="1" Grid.Row="0" Background="Black" Foreground="White"/>
</StackPanel>
<Canvas Grid.Column="0" Grid.Row="0" Grid.RowSpan="2">
<Rectangle Fill="Red" Height="2" Width="110" Canvas.Top="20" Canvas.Left="23.75"/>
<Rectangle Fill="LawnGreen" Height="20" Width="5" Canvas.Top="170" Canvas.Left="76.25" x:Name="line"/>
<Rectangle Fill="Red" Height="2" Width="110" Canvas.Top="190" Canvas.Left="23.75"/>
</Canvas>
<StackPanel Grid.Row="1"/>
</Grid>
</Window>
在我的代碼中,我嘗試了一些來自互聯網的東西。我其實一點頭緒都沒有。
代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp.View
{
/// <summary>
/// Interaktionslogik für CPR.xaml
/// </summary>
public partial class CPR : Window
{
public CPR()
{
InitializeComponent();
var translation = new TranslateTransform(0, -170);
var animation = new DoubleAnimation();
animation.Duration = new Duration(new TimeSpan(625));
animation.RepeatBehavior = RepeatBehavior.Forever;
animation.From = 76.25;
animation.To = 20;
var board = new Storyboard();
board.Children.Add(animation);
Storyboard.SetTarget(board, translation);
Storyboard.SetTargetProperty(board, new PropertyPath(Rectangle.Y));
line.Loaded = delegate (object sender, RoutedEventArgs e)
{
line.BeginStoryboard(board);
};
}
}
}
謝謝你的幫助!
uj5u.com熱心網友回復:
您只需為Canvas.Top
屬性設定影片即可。
以下示例將在 1 秒內將 Top 值從 100 設定為 200,然后再從 200 恢復為 100,直到永遠。
<Canvas>
<Rectangle x:Name="rectangle" Canvas.Left="100" Canvas.Top="100"
Width="100" Height="100" Fill="Red"/>
</Canvas>
在后面的代碼中:
var topAnimation = new DoubleAnimation
{
From = 100,
To = 200,
Duration = TimeSpan.FromSeconds(1),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
rectangle.BeginAnimation(Canvas.TopProperty, topAnimation);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486022.html