Microsoft.UI.Windowing.DisplayArea.FindAll iterator throws "Invalid Cast"
See original GitHub issueDescribe the bug
When using the Microsoft.UI.Windowing.DisplayArea.FindAll
method, the returned collection fails to be iterated but throws an exception.
Steps to reproduce the bug
- Create a new
Blank App, Packaged (WinUI 3 in Desktop)
C# project - Add this code to MainWindow
var displays = Microsoft.UI.Windowing.DisplayArea.FindAll();
- Try and iterate the returned collection (foreach or ToArray)
- Observe exception thrown when run:
Expected behavior
Exception thrown: “Specified cast is not valid.” StackTrace:
at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
at WinRT.IWinRTObject.IsInterfaceImplementedFallback(RuntimeTypeHandle interfaceType, Boolean throwIfNotImplemented)
at WinRT.IWinRTObject.System.Runtime.InteropServices.IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, Boolean throwIfNotImplemented)
at System.Runtime.InteropServices.DynamicInterfaceCastableHelpers.IsInterfaceImplemented(IDynamicInterfaceCastable castable, RuntimeType interfaceType, Boolean throwIfNotImplemented)
at System.Runtime.CompilerServices.CastHelpers.ChkCastAny(Void* toTypeHnd, Object obj)
at ABI.System.Collections.Generic.IEnumerable`1.<>c__DisplayClass11_0.<_FromIterable>b__0()
at WinRT.IWinRTObject.<>c__DisplayClass13_0.<GetOrCreateTypeHelperData>b__0(RuntimeTypeHandle type)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at WinRT.IWinRTObject.GetOrCreateTypeHelperData(RuntimeTypeHandle type, Func`1 helperDataFactory)
at ABI.System.Collections.Generic.IEnumerable`1._FromIterable(IWinRTObject _this)
at ABI.System.Collections.Generic.IEnumerable`1.global::System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at App69.MainWindow..ctor() in E:\sources.tmp\App69\App69\MainWindow.xaml.cs:line 32
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.0 (If you’re seeing your issue in older previews of WinUI 3, please try this release)
Windows app type
- UWP
- Win32
Device form factor
Desktop
Windows version
Windows 11 22000.318
Additional context
As an alternative, you can use PInvoke to get the information, albeit this is a lot more cumbersome: https://github.com/dotMorten/WinUIEx/blob/3b779e5421960357d88ac8c2145d25a9dd13233c/src/WinUIEx/MonitorInfo.cs
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Windows App SDK - DisplayArea.FindAll Method
Retrieves a list of all display areas currently present in the system.
Read more >Winui 3 open a new window in secondary monitor
UWP apps have only one windows while WinUI3 can create new ones. ... UI.Windowing.DisplayArea.FindAll iterator throws "Invalid Cast.
Read more >platform/frameworks/base
When invoked, the system will display the various * {@link DocumentsProvider} instances installed on the device, letting the @@ -4077,6 +4095,34 @@ *...
Read more >Pro C# 2010 and the .NET 4 Platform
Chapter 27: Introducing Windows Presentation Foundation and XAML ... You can find all of the code examples contained in this book available as...
Read more >Developing Web User Interfaces with Oracle ADF Faces
What You May Need to Know About ADF Faces Window Manager. Configuration ... Databound Web User Interface in Developing Fusion Web Applications with....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@deepthi-msft Different issue (albeit perhaps same root cause).
(Edit) @manodasanW /@ujjwalChadha the issue is caused by the below CsWinRT bug: https://github.com/microsoft/CsWinRT/issues/747