0

Flasher という WPF コントロール ライブラリ プロジェクトで定義した WPF カスタム コントロールがあります。基本的には、コンソールの点滅ライトのように、Fill プロパティが 2 つの色の間を行き来する四角形です。ライブラリの 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 ) );
    }
}

これは数か月前はすべて機能していましたが、現在は機能していません。つまり、コントロールを使用するウィンドウで設定した 2 つの色の間でフラッシャーの色が変わるのを見ていました。IsFlashing セッターにブレークポイントを設定しましたが、FindName 呼び出しがグリッドを返していることがわかり、VisualStateManager 呼び出しが機能することがわかっているため、色が変化しない理由がわかりません。これは私をかなり当惑させました。

さらに、Snoop は問題のあるウィンドウを見つけられないようです。これはアプリケーションのメイン ウィンドウではなく、モードレス ポップアップです。基本的に、問題のあるウィンドウは Window から派生し、次のコードで作成および表示されます。

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

だからスヌープはダメだった。

公開したコードに明らかなエラーがない場合は、コードの別の場所で問題を探す必要があります。

ご協力いただきありがとうございます。

トニー

4

1 に答える 1

1

xamlコードを、それが機能していることを知っていたときから以前のバージョンと比較することで、問題の解決策を見つけました。ある時点で、VisualStateManagerのFlashing状態のStoryBoardタグをDynamicResourceに変更したことがわかりました。それが機能したとき、私はそれをStaticResourceに設定しました。StaticResourceに戻すと、再び機能するようになりました。

于 2011-12-30T20:11:41.917 に答える