2

I want to do something that could be a little bit tricky, since it's a new feature in Win 10 Creators Update: I would like to use the new Acrylic Accent feature to make transparent Windows in UWP apps. I saw that Microsoft is already introducing it in Groove and Film & TV in Fast Insider Ring.

This is the code I developed, using examples in Win Dev Center and some other answers here on 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); 
    }
}

Where LayoutRoot is a RelativePanel used as root panel.

But something isn't working: what? How can I apply it to a UIElement, like a Page or a Panel?

I really would appreciate your help, thanks.

Luca Lindholm
  • 813
  • 1
  • 9
  • 23

2 Answers2

3

Solved by myself: had to specify SpriteVisual size manually (making it to fit the UIElement target) and the sizeChanged event of the UIElement itself.

Here is the sample code, I used the generic Panel class (in order to use easily the Panel.ActualWidth/ActualHeight properties...), but every UIElement is ok for the Acrylic Effect:

    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);
    }

and the sizeChanged event associated to the target UIElement (here called 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);
        }
    }

Enjoy.

;D

Luca Lindholm
  • 813
  • 1
  • 9
  • 23
  • Can you please add the code you used to solve your own question? – WJM Apr 11 '17 at 12:41
  • @WJM, did it... enjoy. ;) – Luca Lindholm Apr 14 '17 at 20:42
  • @LucaLindholm Can you give the Link to the "examples in Win Dev Center"? I can't find it – Vijay Nirmal Apr 29 '17 at 10:08
  • @LucaLindholm No need this much of code to achieve Acrylic Accent. See this [Post](http://stackoverflow.com/questions/43699256/how-to-use-acrylic-accent-in-windows-10-creators-update) – Vijay Nirmal May 01 '17 at 10:13
  • 1
    Two things to note here: 1) You don't need the `GaussianBlurEffect` as the `HostBackdropBlur` object already applies a blur effect on its own, and 2) This is *not* the "Acrylic" effect, but just a blurred effect on the screen content behind the app window. The actual Acrylic effect is made up of a series effects such as blur, tint, saturation and noise, it's not just what you can see from your code. – Sergio0694 May 20 '17 at 11:41
  • @Sergio0694 Thank you. I already knew that this wasn't the _complete_ Acrylic Accent. As a matter of fact, I wanted to reproduce even the _noise_ level... any suggestions? – Luca Lindholm May 22 '17 at 16:43
  • I used your solution and it worked the backgroudn of my whole app went transparent and blur as expected but another funny thing which happened was all of my foreground went invisible. meaning all I could see in my app window was transparent app window and nothing else, the ui elements in my ap all of them went 100 percent invisible. do you know how I can fix this ? – Muhammad Touseef Jun 14 '17 at 18:57
  • @touseef The fact is that you don't have to apply it to your "Root Panel", otherwise every child element will disappear! You need to create a child Panel element, put it behind the other children elements and then apply the Acrylic Accent to it. In this way: , and then apply Acrylic Accent to "AcrylicGrid" (this is only example code, obvious... you can rename it how you like, the important thing is that you understand the mechanics). ;) – Luca Lindholm Jun 14 '17 at 19:26
  • so basically I have to create 3 containers for my app? – Muhammad Touseef Jun 15 '17 at 08:03
  • the outer most will be layout root and will be untouched ( with background transparent ). middle one ( child of layout root ) will be the acrylic panel and acrylic will be applied to it. and inner most ( child of acryclic panel ) must contain all the xaml for my app which I want to show right? and I can only put ZIndex to "1" on inner most panel? or do I have to put ZIndex to all nesting and child elements of the inner most panel ? – Muhammad Touseef Jun 15 '17 at 08:05
  • @touseef No, in that way you would repeat the same error. I understand that it is a little bit difficult to explain code sample here in the comment section, where I can't nest the lines, so please read the new answer I've created in this thread. – Luca Lindholm Jun 15 '17 at 20:03
1

Here is the sample XAML code in order to avoid other visual elements disappear when you're going to apply 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>

You shall apply the Acrylic Accent ONLY to the "AcrylicGrid". Remember: all children elements of a UIElement that has Acrylic Accents applied to will "disappear", so the target Acrylic UIElement must not have children.

You can put all the other XAML elements into a one single IPanel element, in order to simplify the Canvas.ZIndex thing, in order to apply it only to the Panel itself.

Obviously this is only a sample snippet, so you can rename those elements with every name you desire.

Hoping to be helpful.

;)

Luca Lindholm
  • 813
  • 1
  • 9
  • 23
  • it works perfect, can you please let me know if there is a way to add some color tint to background blur? I mean yeah I can put another layer on top of it with 0.5 opacity and some color but then the blur effect will be reduced, so is there a way to controlthe blur effect as well and add tint? – Muhammad Touseef Jun 16 '17 at 08:05
  • Not in Creators Update, unfortunately. Acrylic Accent will be customizable in the Fall Creators Update, coming in September. In the Insider builds, you can already experiment the new Acrylic Brushes available as System resources. ;) – Luca Lindholm Jun 16 '17 at 09:40
  • yes ive used them, because I am using insiders build too, but my app minimum target is anniversary update, so I guess I can provide backdrop brush for anniversary, hostbackdrop for creators update and complete arylic for creators fall update, right? – Muhammad Touseef Jun 16 '17 at 09:43
  • Yes, indeed. The only way to customize the Acrylic Accent in CU, for now, is to put on top of it some element with its own background and opacity properties. – Luca Lindholm Jun 16 '17 at 10:07