Power Code
Power Code

Reputation: 21

How to Use Mica/Acrylic Backdrop with Tint Color?

I want to use Mica/Acrylic with Tint Options. There are 2 ways to do this:

First one is available in WinUI3-Gallery and is working fine, however, it seems that there is a better (I don't know!) way to do this, in second option we can inherit SystemBackdrop class and override TargetConnected method:

public class SystemMica : SystemBackdrop
{
    private MicaController micaController;

    public SystemMica()
    {
        micaController = new MicaController();
    }

    protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop connectedTarget, XamlRoot xamlRoot)
    {
        base.OnTargetConnected(connectedTarget, xamlRoot);

        // Set configuration.
        SystemBackdropConfiguration defaultConfig = GetDefaultSystemBackdropConfiguration(connectedTarget, xamlRoot);

        //micaController.TintColor = Colors.Yellow;
        micaController.SetSystemBackdropConfiguration(defaultConfig);
        
        // Add target.
        micaController.AddSystemBackdropTarget(connectedTarget);
    }

    protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop disconnectedTarget)
    {
        base.OnTargetDisconnected(disconnectedTarget);

        micaController.RemoveSystemBackdropTarget(disconnectedTarget);
        micaController = null;
    }
}

If we run above code, we can see mica backdrop and changing Theme To Dark/Light is working fine! However, when we add TintColor:

micaController.TintColor = Colors.Yellow;

and run our app, changing Theme does not reflect to backdrop and backdrop uses previous theme. In ms-docs, it is mentioned that

If FallbackColor, LuminosityOpacity, TintColor, or TintOpacity are modified by the material's implementation, changes to associated SystemBackdropConfiguration.Theme will no longer automatically toggle the controller's theme (because the default Dark and Light configurations are no longer appropriate). In this case, the material's appearance properties need to be manually updated to match the new theme in SystemBackdrop.OnDefaultSystemBackdropConfigurationChanged.

I tried this:

protected override void OnDefaultSystemBackdropConfigurationChanged(ICompositionSupportsSystemBackdrop target, XamlRoot xamlRoot)
    {
        if (target != null)
        {
            SystemBackdropConfiguration defaultConfig = GetDefaultSystemBackdropConfiguration(target, xamlRoot);
            switch (((FrameworkElement)window?.Content).ActualTheme)
                {
                    case ElementTheme.Dark: defaultConfig.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Dark; break;
                    case ElementTheme.Light: defaultConfig.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Light; break;
                    case ElementTheme.Default: defaultConfig.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Default; break;
                }
            micaController.SetSystemBackdropConfiguration(defaultConfig);
        }
    }

but nothing is happening and it does not work!

enter image description here

Upvotes: 0

Views: 231

Answers (0)

Related Questions