我有兩個畫筆。我不知道他們是誰的刷子類型。它們可以是ImageBrushes,SolidBrushes或VisualBrushes。我有每個在「刷」類型的變量。如何在WPF中組合畫筆?
我需要結合兩個畫筆。我該怎麼做?
我試過了。但它沒有奏效。這裏是後面和前面畫筆,需要我結合。
Border Bd = new Border();
Border Bdr = new Border();
Bd.Width = 1.0;
Bd.Height = 1.0;
Bd.Background = Back;
Bdr.Background = Front;
Bd.Child = Bdr;
Brush VB = new VisualBrush(Bd);
我需要這個,因爲我想提出一個自定義動畫類動畫刷。 做了一些測試之後,我得出結論,錯誤在於刷子的組合,而不是在課堂上的其他地方。
生成的畫筆完全透明。
[編輯]
下面是完整BrushAnimation類。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
namespace WPFSoPaTest
{
class BrushAnimation : AnimationTimeline
{
protected override Freezable CreateInstanceCore()
{
return new BrushAnimation();
}
public override Type TargetPropertyType
{
get { return typeof(Brush); }
}
static BrushAnimation()
{
FromProperty = DependencyProperty.Register("From", typeof(Brush),
typeof(BrushAnimation));
ToProperty = DependencyProperty.Register("To", typeof(Brush),
typeof(BrushAnimation));
}
public static readonly DependencyProperty FromProperty;
public Brush From
{
get
{
return (Brush)GetValue(BrushAnimation.FromProperty);
}
set
{
SetValue(BrushAnimation.FromProperty, value);
}
}
public static readonly DependencyProperty ToProperty;
public Brush To
{
get
{
return (Brush)GetValue(BrushAnimation.ToProperty);
}
set
{
SetValue(BrushAnimation.ToProperty, value);
}
}
public override object GetCurrentValue(object defaultOriginValue,
object defaultDestinationValue, AnimationClock animationClock)
{
Brush fromVal = ((Brush)GetValue(BrushAnimation.FromProperty)).CloneCurrentValue();
Brush toVal = ((Brush)GetValue(BrushAnimation.ToProperty)).CloneCurrentValue();
if ((double)animationClock.CurrentProgress == 0.0)
return fromVal; //Here it workes fine.
if ((double)animationClock.CurrentProgress == 1.0)
return toVal; //It workes also here fine.
toVal.Opacity = (double)animationClock.CurrentProgress;
Border Bd = new Border();
Border Bdr = new Border();
Bd.Width = 1.0;
Bd.Height = 1.0;
Bd.Background = fromVal;
Bdr.Background = toVal;
Bd.Visibility = Visibility.Visible;
Bdr.Visibility = Visibility.Visible;
Bd.Child = Bdr;
Brush VB = new VisualBrush(Bd);
return VB; //But here it return's a transparent brush.
//If I return the 'toVal' variable here it animates correctly the opacity.
}
}
}
哎呀我忘了提。爲了做動畫,我經常更換前刷的不透明度。它將在動畫開始時爲0,並且它將以1結束。因此最後它將覆蓋後刷。 它的問題是生成的畫筆完全透明。 – 2009-10-09 08:08:07
也許你不小心設置了VisualBrush「VB」的透明度。用一個不完整的例子很難說出什麼問題。 – bitbonk 2009-10-09 08:52:39
我添加了完整的類。再看一遍。 – 2009-10-09 23:06:31