2017-06-29 16 views
2

我在Windows UI開發實驗室示例庫上使用了示例。我用ShyHeader示例放入我的應用程序,但我沒有使用完全相同的代碼,但我實際上是根據自己的需要編輯了示例。 我的問題是如何使用表達式節點淡入與滾動查看器相對應的特定XAML元素,我可以用滾動查看器淡出某個元素。但我無法從不透明0 - >不透明度1的元素中淡入。 這裏是我的代碼。uwp表達式節點在動畫中淡入淡出

<ScrollViewer x:Name="MyScrollViewer"> 
    <Grid> 
     <local:MyAdaptiveView Margin="0,300,0,0" 
        x:Name="AllVideosGridView"/> 
     <Grid x:Name="Header" Height="300" VerticalAlignment="Top"> 
      <FlipView x:Name="MainFlipView" 
      </FlipView> 
      <Grid Background="Blue" Height="150" VerticalAlignment="Bottom" Opacity="0.5" Name="FrontGrid"> 

      </Grid> 
     </Grid> 
    </Grid> 
</ScrollViewer> 

頁面加載方法

的代碼的唯一重要的一塊只有在這個方法的最後,最後4,5號線,你可以看到我能淡出元素通過做1- progresNode但我嘗試淡入另一個元素(frontVisual)通過做0 + progressNode不起作用,並且即使在滾動後,frontVisual實際上仍保持爲0不透明度。 private void ShyView_Loaded(object sender,RoutedEventArgs e) { //獲取包含MyScrollViewer滾動值的PropertySet _scrollerPropertySet = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(MyScrollViewer); _compositor = _scrollerPropertySet.Compositor;

 // Create a PropertySet that has values to be referenced in the ExpressionAnimations below 
     _props = _compositor.CreatePropertySet(); 
     _props.InsertScalar("progress", 0); 
     _props.InsertScalar("clampSize", 150); 
     _props.InsertScalar("scaleFactor", 0.7f); 

     // Get references to our property sets for use with ExpressionNodes 
     var scrollingProperties = _scrollerPropertySet.GetSpecializedReference<ManipulationPropertySetReferenceNode>(); 
     var props = _props.GetReference(); 
     var progressNode = props.GetScalarProperty("progress"); 
     var clampSizeNode = props.GetScalarProperty("clampSize"); 
     var scaleFactorNode = props.GetScalarProperty("scaleFactor"); 

     // Create a blur effect to be animated based on scroll position 
     var blurEffect = new GaussianBlurEffect() 
     { 
      Name = "blur", 
      BlurAmount = 0.0f, 
      BorderMode = EffectBorderMode.Hard, 
      Optimization = EffectOptimization.Balanced, 
      Source = new CompositionEffectSourceParameter("source") 
     }; 

     var blurBrush = _compositor.CreateEffectFactory(
      blurEffect, 
      new[] { "blur.BlurAmount" }) 
      .CreateBrush(); 

     blurBrush.SetSourceParameter("source", _compositor.CreateBackdropBrush()); 

     // Create a Visual for applying the blur effect 
     _blurredBackgroundImageVisual = _compositor.CreateSpriteVisual(); 
     _blurredBackgroundImageVisual.Brush = blurBrush; 
     _blurredBackgroundImageVisual.Size = new Vector2((float)Header.ActualWidth, (float)Header.ActualHeight); 

     // Insert the blur visual at the right point in the Visual Tree 
     ElementCompositionPreview.SetElementChildVisual(Header, _blurredBackgroundImageVisual); 

     // Create and start an ExpressionAnimation to track scroll progress over the desired distance 
     ExpressionNode progressAnimation = EF.Clamp(-scrollingProperties.Translation.Y/clampSizeNode, 0, 1); 
     _props.StartAnimation("progress", progressAnimation); 

     // Create and start an ExpressionAnimation to animate blur radius between 0 and 15 based on progress 
     ExpressionNode blurAnimation = EF.Lerp(0, 15, progressNode); 
     _blurredBackgroundImageVisual.Brush.Properties.StartAnimation("blur.BlurAmount", blurAnimation); 

     // Get the backing visual for the header so that its properties can be animated 
     Visual headerVisual = ElementCompositionPreview.GetElementVisual(Header); 

     // Create and start an ExpressionAnimation to clamp the header's offset to keep it onscreen 
     ExpressionNode headerTranslationAnimation = EF.Conditional(progressNode < 1, 0, -scrollingProperties.Translation.Y - clampSizeNode); 
     headerVisual.StartAnimation("Offset.Y", headerTranslationAnimation); 

     // Create and start an ExpressionAnimation to scale the header during overpan 
     ExpressionNode headerScaleAnimation = EF.Lerp(1, 1.25f, EF.Clamp(scrollingProperties.Translation.Y/50, 0, 1)); 
     headerVisual.StartAnimation("Scale.X", headerScaleAnimation); 
     headerVisual.StartAnimation("Scale.Y", headerScaleAnimation); 

     //Set the header's CenterPoint to ensure the overpan scale looks as desired 
     headerVisual.CenterPoint = new Vector3((float)(Header.ActualWidth/2), (float)Header.ActualHeight, 0); 

     // Get the backing visual for the photo in the header so that its properties can be animated 
     Visual photoVisual = ElementCompositionPreview.GetElementVisual(MainFlipView); 

     // Create and start an ExpressionAnimation to opacity fade out the image behind the header 
     ExpressionNode imageOpacityAnimation = 1 - progressNode; 
     photoVisual.StartAnimation("opacity", imageOpacityAnimation); 

     // Get the front visual for the photo in the header so that its properties can be animated 
     Visual frontVisual = ElementCompositionPreview.GetElementVisual(FrontGrid); 

     // Create and start an ExpressionAnimation to opacity fade out the image behind the header 
     ExpressionNode imageOpacityAnimation2 = 0 + progressNode; 
     frontVisual.StartAnimation("opacity", imageOpacityAnimation2); 
    } 

注意行爲其實我是想的是,當我向下滾動,然後FlipView應該淡出,當我滾動到頂部應該淡入,這是可以正常使用,但隨着它我想FrontGrid是完全相反的,即:淡入淡出和向上滾動淡出。

預先感謝

回答

2

你表達看起來OK。

請注意您正在用動畫製作的Opacity構圖是VisualOpacity。但是,您正在設置的0.5OpacityFrontGrid XAML來自UIElement。這樣做會破壞構圖不透明度表達動畫。

解決方法是簡單的 - 嘗試獲得正確後您FrontGridVisualInitializeComponent並設置其Opacity0.5存在(即frontVisual.Opacity = 0.5),而不是在XAML中設置它。


你會看到這種來自週年更新開始,由於XAML的組成互操作行爲改變「怪異」的行爲。請致電official document

總之,XAML不知道Composition是否改變了Opacity,它仍然認爲它應該是0.5,因爲它是上次設置的。所以它會嘗試覆蓋並導致動畫失敗。這也發生在OffsetSize之類的其他一些屬性上。

我的建議是,如果你去組成,嘗試去所有的方式組成。 :)