2017-04-04 106 views
2

我想做一些可能有点棘手的事情,因为它是Win 10 Creators Update中的一项新功能:我想使用新的Acrylic Accent功能在UWP应用程序中制作透明的Windows。 我看到微软已经在Fast Insider Ring中引入了Groove和Film &电视。如何在Windows 10 Creators中正确使用Acrylic Accent(CreateHostBackDropBrush())?

这是我开发的,采用在Win开发中心的例子和一些其他的答案在这里堆栈溢出代码:

public sealed partial class MainPage : Page 
{ 
    // Some other things 

    private void initializeInterface() 
    { 

     /// Applying Acrylic Accent 
     LayoutRoot.Background = null; 

     GaussianBlurEffect blurEffect = new GaussianBlurEffect() 
     { 
      Name = "Blur", 
      BlurAmount = 5.0f, 
      BorderMode = EffectBorderMode.Hard, 
      Optimization = EffectOptimization.Balanced, 
      Source = new CompositionEffectSourceParameter("source"), 
     }; 

     LayoutRoot.Background = null; 

     var rootVisual = ElementCompositionPreview.GetElementVisual(LayoutRoot as UIElement); 
     var compositor = rootVisual.Compositor; 
     var factory = compositor.CreateEffectFactory(blurEffect); 
     var effectBrush = factory.CreateBrush(); 

     // This is the effect I wanted to use, the "Acrylic Accent", as it is called by MS itself. 
     var backdropBrush = compositor.CreateHostBackdropBrush();    
     effectBrush.SetSourceParameter("source", backdropBrush); 

     var blurVisual = compositor.CreateSpriteVisual(); 
     blurVisual.Brush = effectBrush; 
     ElementCompositionPreview.SetElementChildVisual(LayoutRoot as UIElement, blurVisual); 
    } 
} 

哪里LayoutRoot是用作根面板RelativePanel

但是有些东西不起作用:什么? 如何将它应用于UIElement,如PagePanel

我真的很感谢你的帮助,谢谢。

回答

2

自己解决:必须手动指定SpriteVisual大小(使其适合UIElement目标)和UIElement本身的sizeChanged事件。

下面是示例代码,我使用的通用Panel类(为了方便地使用Panel.ActualWidth/ActualHeight属性...),但每UIElement是确定的亚克力效果:

private Compositor compositor; 
    private SpriteVisual hostVisual; 

    private void applyAcrylicAccent(Panel e) 
     { 
      compositor = ElementCompositionPreview.GetElementVisual(e).Compositor; 
      hostVisual = compositor.CreateSpriteVisual(); 
      hostVisual.Size = new System.Numerics.Vector2((float)e.ActualWidth, (float)e.ActualHeight); 

     // You can choose which effect you want, it is indifferent 
     GaussianBlurEffect blurEffect = new GaussianBlurEffect() 
     { 
      Name = "Blur", 
      BlurAmount = 0.0f, 
      BorderMode = EffectBorderMode.Hard, 
      Optimization = EffectOptimization.Balanced, 
      Source = new CompositionEffectSourceParameter("source"), 
     }; 

     var factory = compositor.CreateEffectFactory(blurEffect, null); 
     var effectBrush = factory.CreateBrush(); 

     effectBrush.SetSourceParameter("source", compositor.CreateHostBackdropBrush()); 

     hostVisual.Brush = effectBrush; 
     ElementCompositionPreview.SetElementChildVisual(e, hostVisual); 
    } 

sizeChanged与目标UIElement(此处称为LayoutRoot)相关的事件:

private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e) 
    { 
     if (hostVisual != null) 
     { 
      hostVisual.Size = new System.Numerics.Vector2((float)e.NewSize.Width, (float)e.NewSize.Height); 
     } 
    } 

享受。

; d

+0

能否请你加你用来解决自己的问题的代码? – WJM

+0

WJM,现在就做,享受。 ;) – LucaLindholm

+0

@WJM,做到了......享受。 ;) – LucaLindholm

1

这是为了避免样品XAML代码,当你打算申请亚克力口音其他视觉元素消失:

<SomePanelSubclass "rootGrid"> 
     <SomePanelSubclassForAcrylic> 
      <SomePanelSubclass "AcrylicGrid"/> 
      <!-- Comment: some background is needed in order to make elements more visible --> 
      <SomeElementWithBackgroundProperty Background="SomeBrush" Canvas.ZIndex="ValueAboveZero"/> 
     </SomePanelSubclassForAcrylic> 
     <AllOtherChildrenElements Canvas.ZIndex="ValueAboveZero"/> 
    </SomePanelSubclass> 

你应适用亚克力口音仅对“AcrylicGrid”。 请记住:所有应用了丙烯酸口音的UIElement的所有儿童元素都将“消失”,因此目标丙烯酸UIElement不得有子女。

你可以把其他所有的XAML元素成一个单一的茁壮元素,以简化Canvas.ZIndex东西,为了它仅适用于面板本身。

显然,这仅仅是一个示例代码段,这样你就可以重命名每一个你想要的名称的那些元素。

希望能有所帮助。

;)

+0

它的工作原理非常完美,您是否可以让我知道是否有办法为背景模糊添加一些色调?我的意思是,我可以用0.5不透明度和一些颜色在其上再铺一层,但是模糊效果会降低,那么是否有办法控制模糊效果并添加色调? – touseef

+0

不幸的是在创作者更新中。秋季创作者更新将于9月份定制丙烯酸口音。在Insider构建中,您可以尝试使用新的丙烯酸刷作为系统资源。 ;) – LucaLindholm

+0

是的,我使用它们,因为我使用内部人员也建立,但我的应用程序的最低目标是周年纪念更新,所以我想我可以提供周年纪念背景画笔,为创作者更新和完成arylic为创作者更新的主机,对吧? – touseef

相关问题