2011-12-27 4 views
0

ではありませんました。基本的に、Fillプロパティがコンソール上の点滅するライトのように2つの色の間で前後に点滅する四角形です。ここでは、ライブラリのためのGeneric.xamlファイル内にある制御のためのテンプレートがあります:WPFキーフレームアニメーションが動作しますが、今、私は私がフラッシャーと呼ばれるWPFコントロールライブラリプロジェクトで定義されたWPFカスタムコントロールを持って

<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が仕事を呼び出すことを知っているので、私は色が変化し見ていないよ、なぜ私は理解していません。これは私にはかなり戸惑いました。

プラススヌープは、問題が発生しているウィンドウを見つけるように見えることはできません。私のアプリケーションのメインウィンドウではなく、モードレスのポップアップです。基本的に、問題を持つウィンドウがウィンドウから下降し、次のコードを使用して作成し、表示されます。

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

ので、スヌープは無用となっています。

私が公開したコードには明白な誤りがない場合、私は問題の私のコードの他の場所で見てする必要があります。

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

トニー

答えて

1

私はそれが働いていた知っていた時から、以前のバージョンにXAMLコードを比較することによって、問題の解決策を発見しました。ある時点で、Visual State ManagerのFlashing状態のStoryBoardタグをDynamicResourceに変更したことが判明しました。それがうまくいったら、StaticResourceに設定しました。それをStaticResourceに戻すと、再び動作します。

関連する問題