Skip to content

SurfaceLoader.LoadImageAsync always fails in WinUI 3 #233

Closed

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.

Activity

  1. michael-hawker commented on Sep 19, 2023

    @michael-hawker
    Member

    Thanks for the report @Marv51, yeah, this got missed in the port. Not sure how as we were comparing against the winui branch. FYI @Arlodotexe.

  2. moved this to 🔖 Ready in Toolkit 8.xon Sep 19, 2023
  3. moved this from 🔖 Ready to 🆕 New in Toolkit 8.xon Nov 7, 2023
  4. moved this from 🆕 New to 🏗 In progress in Toolkit 8.xon Nov 7, 2023
  5. Arlodotexe commented on Nov 7, 2023

    @Arlodotexe
    Member

    Possibly related to #235

  6. moved this from 🏗 In progress to 👀 In review in Toolkit 8.xon Dec 12, 2023
  7. moved this from 👀 In review to ✅ Done in Toolkit 8.xon Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingregressionWhat was working is now broke

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions