2011-12-27 2 views
0

WPF 컨트롤 라이브러리 프로젝트에서 정의한 WPF 사용자 정의 컨트롤이 있습니다. 기본적으로 Fill 속성이 콘솔의 깜박이는 빛처럼 두 색상 사이에서 앞뒤로 번쩍 거리는 사각형입니다. 여기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> 

코드 숨김 년대 제어 : 다음은 라이브러리의 generic.xaml을 파일에 컨트롤의 템플릿,의

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 setter에 중단 점을 설정했는데 FindName 호출이 Grid를 반환하고 VisualStateManager 호출이 작동한다는 것을 알고 있으므로 왜 색상이 변경되지 않는지 이해할 수 없습니다. 이것은 나에게 당황 스럽다.

플러스 스누핑 문제가있는 창을 찾을 수 없습니다. 그것은 내 응용 프로그램의 메인 윈도우가 아니라 모덜리스 팝업입니다. 기본적으로 문제가있는 창은 Window에서 내려와 다음 코드로 생성되고 표시됩니다.

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

그래서 Snoop은 쓸모가 없습니다.

게시 한 코드에 눈부신 오류가없는 경우 문제의 코드에서 다른 부분을 찾아야합니다.

도움을 주셔서 감사합니다. 나는 그것이를 정상적으로 작동 할 때

토니

답변

1

나는 시간에서 이전 버전으로 XAML 코드를 비교하여 문제에 대한 해결책을 발견했다. 어떤 시점에서 Visual State Manager의 Flashing 상태에서 StoryBoard 태그를 DynamicResource로 변경했습니다. 작동하면 StaticResource로 설정했습니다. 다시 StaticResource로 변경하면 다시 작동합니다.

관련 문제