question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Microsoft.UI.Windowing.DisplayArea.FindAll iterator throws "Invalid Cast"

See original GitHub issue

Describe 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

  1. Create a new Blank App, Packaged (WinUI 3 in Desktop) C# project
  2. Add this code to MainWindow var displays = Microsoft.UI.Windowing.DisplayArea.FindAll();
  3. Try and iterate the returned collection (foreach or ToArray)
  4. 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:open
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dotMortencommented, Aug 1, 2023

@deepthi-msft Different issue (albeit perhaps same root cause).

0reactions
deepthi-msftcommented, Aug 1, 2023

(Edit) @manodasanW /@ujjwalChadha the issue is caused by the below CsWinRT bug: https://github.com/microsoft/CsWinRT/issues/747

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found