Search code examples
c#uwpwindows-10windows-10-universalwindows-composition-api

Getting Access is denied exception when calling CompositionCapabilities.GetForCurrentView


I am getting the following exception when I call CompositionCapabilities.GetForCurrentView.

System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;

    // Exception happens here.
    var capabilities = CompositionCapabilities.GetForCurrentView();
}

The weird thing is the code compiles OK so I assume the API is available. Do I need to declare any capabilities in Package.appxmanifest?


Solution

  • You don't need to declare anything. The method is simply called too early.

    So instead of calling it in the constructor, call it right after the Window is created -

    protected override void OnWindowCreated(WindowCreatedEventArgs args)
    {
        if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 4))
        {
            var capabilities = CompositionCapabilities.GetForCurrentView();
            var areEffectsSupported = capabilities.AreEffectsSupported();
            var areEffectsFast = capabilities.AreEffectsFast();
        }
    
        base.OnWindowCreated(args);
    }
    

    Note you will want to add a check to see if that API is supported too, like in the code above.