0

我在名为 Flasher 的 WPF 控件库项目中定义了一个 WPF 自定义控件。基本上,它是一个矩形,其 Fill 属性在两种颜色之间来回闪烁,就像控制台上的闪烁灯一样。这是控件的模板,它位于库的 Generic.xaml 文件中:

<Style TargetType="{x:Type local:Flasher}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Flasher}">
                <Grid Name="LayoutRoot">
                    <Grid.Resources>
                        <Storyboard x:Key="FlashingStoryboard" AutoReverse="True" RepeatBehavior="Forever">
                            <ColorAnimationUsingKeyFrames BeginTime="00:00:00" 
                                                          Storyboard.TargetName="Flasher" 
                                                          Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <LinearColorKeyFrame KeyTime="00:00:00.5" 
                                                     Value="{Binding Path=FlashColor, RelativeSource={RelativeSource AncestorType={x:Type local:Flasher}}}"/>
                            </ColorAnimationUsingKeyFrames>
                            <DoubleAnimation Duration="00:00:00.05" 
                                             From="0" To="10" 
                                             Storyboard.TargetName="FlasherBlur" 
                                             Storyboard.TargetProperty="Radius">
                            </DoubleAnimation>
                        </Storyboard>
                    </Grid.Resources>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="FlashStates">
                            <VisualState x:Name="Flashing" Storyboard="{DynamicResource ResourceKey=FlashingStoryboard}"/>
                            <VisualState x:Name="Stopped"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <Rectangle Fill="{TemplateBinding Fill}" 
                               Name="Flasher" 
                               Stroke="{TemplateBinding Stroke}" 
                               StrokeThickness="{TemplateBinding StrokeThickness}">
                        <Rectangle.Effect>
                            <BlurEffect x:Name="FlasherBlur" Radius="0"  />
                        </Rectangle.Effect>
                    </Rectangle>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是控件的代码隐藏:

public partial class Flasher : Control {

    public static readonly DependencyProperty FillProperty =
        DependencyProperty.Register( "Fill", typeof( Brush ), typeof( Flasher),
                                     new FrameworkPropertyMetadata ( new SolidColorBrush( Colors.Silver), FrameworkPropertyMetadataOptions.AffectsRender ) );

    public static readonly DependencyProperty FlashColorProperty =
        DependencyProperty.Register( "FlashColor", typeof( Color ), typeof( Flasher ),
                                     new FrameworkPropertyMetadata( Colors.Transparent, FrameworkPropertyMetadataOptions.AffectsRender ) );

    public static readonly DependencyProperty FlashDurationProperty =
        DependencyProperty.Register( "FlashDuration", typeof( TimeSpan ), typeof( Flasher ), new FrameworkPropertyMetadata( TimeSpan.MinValue ) );

    public static readonly DependencyProperty StrokeProperty =
        DependencyProperty.Register( "Stroke", typeof( Brush ), typeof( Flasher ),
                                     new FrameworkPropertyMetadata( new SolidColorBrush( Colors.Silver ), FrameworkPropertyMetadataOptions.AffectsRender ) );

    public static readonly DependencyProperty StrokeThicknessProperty =
        DependencyProperty.Register( "StrokeThickness", typeof( double ), typeof( Flasher ),
                                     new FrameworkPropertyMetadata( 0.0, FrameworkPropertyMetadataOptions.AffectsRender ) );

    protected Application App {
        get { return Application.Current; }
    }

    protected ILog Log {
        get { return (ILog) App.Properties[ "Log" ]; }
    }

    public Brush Fill {
        get { return (Brush) GetValue( FillProperty ); }
        set { SetValue( FillProperty, value ); }
    }

    public Color FlashColor {
        get { return (Color) GetValue( FlashColorProperty ); }
        set { SetValue( FlashColorProperty, value ); }
    }

    public TimeSpan FlashDuration {
        get { return (TimeSpan) GetValue( FlashDurationProperty ); }
        set { SetValue( FlashDurationProperty, value ); }
    }

    private bool flashing = false;

    public bool IsFlashing {
        get { return flashing; }
        set {
            flashing = value;
            FrameworkElement grid = Template.FindName( "LayoutRoot", this ) as FrameworkElement;
            if ( flashing ) {
                if ( !VisualStateManager.GoToElementState( grid, "Flashing", true ) ) {
                    Log.Debug( "Flasher.cs:  Visual State Manager transition failed." );
                }
                if ( FlashDuration > TimeSpan.MinValue ) {
                    ThreadPool.QueueUserWorkItem( WaitForDuration, FlashDuration );
                }
            } else {
                if ( !VisualStateManager.GoToElementState( grid, "Stopped", true ) ) {
                    Log.Debug( "Flasher.cs:  Visual State Manager transition failed." );
                }
            }
        }
    }

    public Brush Stroke {
        get { return (Brush) GetValue( StrokeProperty ); }
        set { SetValue( StrokeProperty, value ); }
    }

    public double StrokeThickness {
        get { return (double) GetValue( StrokeThicknessProperty ); }
        set { SetValue( StrokeThicknessProperty, value ); }
    }

    public Flasher() : base() {}

    static Flasher() {
        DefaultStyleKeyProperty.OverrideMetadata( typeof( Flasher ), new FrameworkPropertyMetadata( typeof( Flasher ) ) );
    }

    private void TurnFlashingOff() {
        // Set IsFlashing to false
        IsFlashing = false;
    }

    private void WaitForDuration( object state ) {
        System.Threading.Thread.Sleep( (TimeSpan) state );

        Dispatcher.BeginInvoke( new Action( TurnFlashingOff ) );
    }
}

几个月前这一切都有效,但现在不起作用。也就是说,我曾经看到闪光灯在我在使用控件的窗口中设置的两种颜色之间改变颜色。我在 IsFlashing 设置器中设置了断点,并且我知道 FindName 调用正在返回 Grid,并且我知道 VisualStateManager 调用有效,所以我不明白为什么我没有看到颜色发生变化。这让我很困惑。

Plus Snoop 似乎无法找到有问题的窗口。它不是我的应用程序的主窗口,而是一个无模式的弹出窗口。本质上,有问题的窗口来自 Window 并使用以下代码创建和显示:

if ( Window == null ) {
    Window = new MyDialog();
    // Set some program-specific window properties that don't affect the display here . . .
    Window.Show();
}

所以Snoop一直没用。

如果我发布的代码中没有明显的错误,那么我将不得不在我的代码中的其他地方查找该问题。

谢谢你提供的所有帮助。

托尼

4

1 回答 1

1

通过将 xaml 代码与我知道它可以工作的早期版本进行比较,我找到了问题的解决方案。事实证明,在某些时候,我将 Visual State Manager 的 Flashing 状态中的 StoryBoard 标签更改为 DynamicResource;当它工作时,我将它设置为静态资源。将其更改回 StaticResource 使其再次工作。

于 2011-12-30T20:11:41.917 回答