Skip to content

SurfaceLoader.LoadImageAsync always fails in WinUI 3 #233

@Marv51

Description

@Marv51

Describe the bug

I am trying to port my Toolkit 7 usage over to version 8. I am using a PipelineBuilder to create a tiled brush.

This fails currently (was working before) with a NullReferenceException.

I have traced back the origin of that exception to this line in the SurfaceLoader

var compositor = Window.Current.Compositor;

There must be another way to get the Compositor in WinUI 3, because Window.Current is always null in WinUI 3.

Not sure if this is still the preferred approach, but in the old repo, the WinUI branch looks like this:

var compositor = CompositionTarget.GetCompositorForCurrentThread();

https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/a10204e648c372f6a6837c5aa40065761847132e/CommunityToolkit.WinUI.UI.Media/Helpers/SurfaceLoader.cs#L45C13-L45C80

Steps to reproduce

This is a pretty obviously an oversight when porting the UWP code, but here is my code just in case:

Uri _transparentBackground = new Uri("ms-appx:///Images/TransparentBackground.png");

var builder = PipelineBuilder.FromTiles(_transparentBackground, DpiMode.DisplayDpiWith96AsLowerBound);
var brush = builder
            .Shade(Colors.Red, 0.5f)
            .AsBrush();

Expected behavior

No exception expected.

Screenshots

No response

Code Platform

  • UWP
  • WinAppSDK / WinUI 3
  • Web Assembly (WASM)
  • Android
  • iOS
  • MacOS
  • Linux / GTK

Windows Build Number

  • Windows 10 1809 (Build 17763)
  • Windows 10 1903 (Build 18362)
  • Windows 10 1909 (Build 18363)
  • Windows 10 2004 (Build 19041)
  • Windows 10 20H2 (Build 19042)
  • Windows 10 21H1 (Build 19043)
  • Windows 10 21H2 (Build 19044)
  • Windows 10 22H2 (Build 19045)
  • Windows 11 21H2 (Build 22000)
  • Other (specify)

Other Windows Build number

No response

App minimum and target SDK version

  • Windows 10, version 1809 (Build 17763)
  • Windows 10, version 1903 (Build 18362)
  • Windows 10, version 1909 (Build 18363)
  • Windows 10, version 2004 (Build 19041)
  • Windows 10, version 2104 (Build 20348)
  • Windows 11, version 22H2 (Build 22000)
  • Other (specify)

Other SDK version

No response

Visual Studio Version

No response

Visual Studio Build Number

No response

Device form factor

No response

Additional context

I found a lot of Window.Current.Compositor instances in this part of the code.

Is a #if WINUI3 the preferred approach?

Help us help you

I'd prefer someone else to look at this, so that it can be addressed in a timely manner, because I am not at all familiar with the new tooling.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingregressionWhat was working is now broke

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions