Windows icon indicating copy to clipboard operation
Windows copied to clipboard

SurfaceLoader fails in WinUI because of unsupported API usage

Open Marv51 opened this issue 1 year ago • 1 comments

Describe the bug

In the latest Toolkit from Jan 24 (8.0.240109), SurfaceLoader fails to create a brush, and it will always return null on WinUI 3.

This is because the private SurfaceLoader.LoadSurfaceBrushAsync method unconditionally calls DisplayInformation.GetForCurrentView(). https://github.com/CommunityToolkit/Windows/blob/2735c8718984daa2ddee7a5aa9235f57bead0df7/components/Media/src/Helpers/SurfaceLoader.cs#L103 This is not supported on WinUI 3 and throws a COMException. This exception gets handled, but an empty/null brush will always be returned.

Steps to reproduce

// The Uri does not matter, it throws before loading anything.
_ = await SurfaceLoader.LoadImageAsync(new Uri("https://example.com"), CommunityToolkit.WinUI.Media.DpiMode.DisplayDpiWith96AsLowerBound);

// The exception is handled so it will return `null`.

Expected behavior

No exception is expected.

The old-7.1 winUI branch worked for me. On that branch this dpi issue was commented out:

https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/080bdb99e6810eb2157ad635f00e64a64acabb50/CommunityToolkit.WinUI.UI.Media/Helpers/SurfaceLoader.cs#L99

Screenshots

No response

Code Platform

  • [ ] UWP
  • [X] 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)
  • [X] Windows 11 21H2 (Build 22000)
  • [ ] Other (specify)

Other Windows Build number

No response

App minimum and target SDK version

  • [X] 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)
  • [X] Windows 11, version 22H2 (Build 22000)
  • [ ] Other (specify)

Other SDK version

No response

Visual Studio Version

2022

Visual Studio Build Number

No response

Device form factor

No response

Additional context

It might be tempting to think that this is the same issue as #233, however it is not. This is about 10 lines further into SurfaceLoader and this wasn't fixed by #299 in the latest update.

Help us help you

No, I'm unable to contribute a solution.

Marv51 avatar Jan 12 '24 14:01 Marv51

Should I submit a PR with the old 7.x "fix" of commenting out the DPI references?

Marv51 avatar Jun 24 '24 11:06 Marv51