繁体   English   中英

如何正确使用Windows 10 Creators Update中的Acrylic Accent(CreateHostBackDropBrush())?

[英]How to use Acrylic Accent (CreateHostBackDropBrush()) in Windows 10 Creators Update correctly?

我想做点棘手的事情,因为它是Win 10 Creators Update中的一项新功能:我想使用新的Acrylic Accent功能在UWP应用中制作透明Windows。 我看到微软已经在Fast Insider Ring的Groove和影视中引入了它。

这是我开发的代码,使用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 (如PagePanel

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

由我自己解决:必须手动指定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相关的sizeChanged事件(在此称为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

这是示例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>

您应仅将丙烯酸口音应用于“ AcrylicGrid”。 请记住:应用了丙烯酸重音符号的UIElement的所有子元素都将“消失”,因此目标Acrylic UIElement不得包含子元素。

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

显然,这只是一个示例片段,因此您可以使用所需的每个名称重命名这些元素。

希望对您有所帮助。

;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM