c#创建画布_如何在C#中为画布上的线设置动画?

这篇博客展示了如何在C#中利用MVVM模式为Canvas上的线条设置动画效果。通过一个运行示例,详细介绍了如何创建线的视图模型,包括坐标、线宽、透明度属性,并使用计时器实现线条的随机平移动画。此外,还提到了如何调整动画速度以及实现通知属性变化的功能。
摘要由CSDN通过智能技术生成

我有一个运行的示例,它使用MVVM模式并在其中创建ListBox具有Canvasas的行ItemsPanel。

我实际上是为这个问题而做的,但OP有点消失了,从来没有和我联系过。

这就是我的电脑中的样子:

它的主要部分是:

X2="{Binding X2}" Y2="{Binding Y2}"

StrokeThickness="{Binding Thickness}"

Opacity="{Binding Opacity}"

x:Name="Line">

视图模型:public class LineViewModel : INotifyPropertyChanged

{

#region Timer-based Animation

private System.Threading.Timer Timer;

private static Random Rnd = new Random();

private bool _animate;

public bool Animate

{

get { return _animate; }

set

{

_animate = value;

NotifyPropertyChanged("Animate");

if (value)

StartTimer();

else

StopTimer();

}

}

private int _animationSpeed = 1;

public int AnimationSpeed

{

get { return _animationSpeed; }

set

{

_animationSpeed = value;

NotifyPropertyChanged("AnimationSpeed");

if (Timer != null)

Timer.Change(0, 100/value);

}

}

private static readonly List _animationSpeeds = new List{1,2,3,4,5};

public List AnimationSpeeds

{

get { return _animationSpeeds; }

}

public void StartTimer()

{

StopTimer();

Timer = new Timer(x => Timer_Tick(), null, 0, 100/AnimationSpeed);

}

public void StopTimer()

{

if (Timer != null)

{

Timer.Dispose();

Timer = null;

}

}

private void Timer_Tick()

{

X1 = X1 + Rnd.Next(-2, 3);

Y1 = Y1 + Rnd.Next(-2, 3);

X2 = X2 + Rnd.Next(-2, 3);

Y2 = Y2 + Rnd.Next(-2, 3);

}

#endregion

#region Coordinates

private double _x1;

public double X1        {

get { return _x1; }

set

{

_x1 = value;

NotifyPropertyChanged("X1");

}

}

private double _y1;

public double Y1        {

get { return _y1; }

set

{

_y1 = value;

NotifyPropertyChanged("Y1");

}

}

private double _x2;

public double X2        {

get { return _x2; }

set

{

_x2 = value;

NotifyPropertyChanged("X2");

}

}

private double _y2;

public double Y2        {

get { return _y2; }

set

{

_y2 = value;

NotifyPropertyChanged("Y2");

}

}

#endregion

#region Other Properties

private string _name;

public string Name

{

get { return _name; }

set

{

_name = value;

NotifyPropertyChanged("Name");

}

}

private double _thickness;

public double Thickness

{

get { return _thickness; }

set

{

_thickness = value;

NotifyPropertyChanged("Thickness");

}

}

public Color Color1 { get; set; }

public Color Color2 { get; set; }

private double _opacity = 1;

public double Opacity

{

get { return _opacity; }

set

{

_opacity = value;

NotifyPropertyChanged("Opacity");

}

}

#endregion

#region INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)

{

Application.Current.Dispatcher.BeginInvoke((Action)(() =>

{

if (PropertyChanged != null)

PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

}));

}

#endregion

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值