【问题标题】:How to use Acrylic Accent (CreateHostBackDropBrush()) in Windows 10 Creators Update correctly?如何在 Windows 10 Creators Update 中正确使用 Acrylic Accent (CreateHostBackDropBrush())?
【发布时间】:2017-08-29 17:58:17
【问题描述】:

我想做一些可能有点棘手的事情,因为它是 Win 10 创意者更新中的一项新功能:我想使用新的 Acrylic Accent 功能在 UWP 应用程序中制作透明的 Windows。 我看到微软已经在 Fast Insider Ring 的 Groove 和 Film & TV 中引入了它。

这是我开发的代码,使用 Win Dev Center 中的示例和 Stack Overflow 上的其他答案:

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,例如Page 或Panel?

非常感谢您的帮助,谢谢。

【问题讨论】:

    标签: c# uwp windows-composition-api


    【解决方案1】:

    自己解决:必须手动指定 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);
        }
    

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

    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

    【讨论】:

    • 你能添加你用来解决你自己问题的代码吗?
    • @WJM,做到了……享受吧。 ;)
    • @LucaLindholm 您能否提供“Win 开发中心中的示例”的链接?我找不到它
    • @LucaLindholm 不需要这么多代码来实现 Acrylic Accent。看到这个Post
    • 这里需要注意两点:1) 你不需要 GaussianBlurEffect,因为 HostBackdropBlur 对象已经自己应用了模糊效果,2) 这是 not “亚克力”效果,但只是应用程序窗口后面的屏幕内容的模糊效果。实际的 Acrylic 效果是由模糊、色调、饱和度和噪点等一系列效果组成的,这不仅仅是您从代码中看到的效果。
    【解决方案2】:

    这是示例 XAML 代码,以避免在您要应用 Acrylic Accent 时其他视觉元素消失:

        <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>
    

    您只能将 Acrylic Accent 应用于“AcrylicGrid”。 请记住:应用了 Acrylic Accents 的 UIElement 的所有子元素都将“消失”,因此目标 Acrylic UIElement 不能有子元素。

    您可以将所有其他 XAML 元素放入一个单独的 IPanel 元素中,以简化 Canvas.ZIndex 事物,以便仅将其应用于面板本身。

    显然这只是一个示例 sn-p,因此您可以使用您想要的每个名称重命名这些元素。

    希望对您有所帮助。

    ;)

    【讨论】:

    • 效果很好,能否告诉我是否有办法为背景模糊添加一些颜色?我的意思是,是的,我可以在其上放置另一层,不透明度为 0.5 和一些颜色,但是模糊效果会降低,那么有没有办法控制模糊效果并添加色调?
    • 很遗憾,不在创作者更新中。 Acrylic Accent 将在 9 月推出的秋季创作者更新中进行定制。在 Insider 版本中,您已经可以试验可用作系统资源的新亚克力笔刷。 ;)
    • 是的,我用过它们,因为我也在使用 Insiders build,但是我的应用程序最低目标是周年纪念更新,所以我想我可以为周年纪念提供背景画笔,为创作者更新提供 hostbackdrop,为创作者提供完整的 arylic秋季更新,对吧?
    • 是的,确实如此。目前,在 CU 中自定义 Acrylic Accent 的唯一方法是在其上放置一些具有自己的背景和不透明度属性的元素。
    猜你喜欢
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多