CommunityToolkit / CommunityToolkit/Windows
SurfaceLoader fails in WinUI because of unsupported API usage
- Dominant language
- C#
- Stars
- 1.1k
- Forks
- 166
- PR merge metrics
- No merged PRs in 30d
Description
### 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
```csharp
// 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.
Contributor guide
Assessment
This issue has not been assessed yet.