dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

[Windows] Adding event handler for DeviceDisplay.MainDisplayInfoChanged Error #4125

Closed PedroCigaw closed 2 years ago

PedroCigaw commented 2 years ago

Description

Calling DeviceDisplay.MainDisplayInfoChanged += DeviceDisplay_MainDisplayInfoChanged causes COMException on windowss

Steps to Reproduce

  1. Create Maui app
  2. In App.xaml.cs after MainPage is set
  3. Add event handler DeviceDisplay.MainDisplayInfoChanged += DeviceDisplay_MainDisplayInfoChanged
  4. Run on windows local machine

Version with bug

Preview 11

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

Window SDK 10.0.19041.0

Did you find any workaround?

{"Element not found. (0x80070490)"} Data: {System.Collections.ListDictionaryInternal} ErrorCode: -2147023728 HResult: -2147023728 HelpLink: null InnerException: null Message: "Element not found. (0x80070490)" Source: "WinRT.Runtime" StackTrace: " at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)\r\n at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)\r\n at ABI.Windows.Graphics.Display.IDisplayInformationStaticsMethods.GetForCurrentView(IObjectReference _obj)\r\n at Windows.Graphics.Display.DisplayInformation.GetForCurrentView()\r\n at Microsoft.Maui.Essentials.DeviceDisplayImplementation.GetMainDisplayInfo(DisplayInformation di)\r\n at Microsoft.Maui.Essentials.DeviceDisplayImplementation.GetMainDisplayInfo()\r\n at Microsoft.Maui.Essentials.DeviceDisplay.add_MainDisplayInfoChanged(EventHandler1 value)\r\n at ) in App.OnStart() in App.xaml.cs:line 81\r\n at Microsoft.Maui.Controls.Application.SendStart()\r\n at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created()\r\n at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.b1_0(Window window)\r\n at Microsoft.Maui.Platform.ApplicationExtensions.<>c__DisplayClass1_0.b1(OnWindowCreated del)\r\n at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[TDelegate](IServiceProvider services, Action1 action)\r\n at Microsoft.Maui.Platform.ApplicationExtensions.CreateNativeWindow(Application nativeApplication, IApplication application, OpenWindowRequest args)\r\n at Microsoft.Maui.Platform.ApplicationExtensions.CreateNativeWindow(Application nativeApplication, IApplication application, LaunchActivatedEventArgs args)\r\n at Microsoft.Maui.MauiWinUIApplication.OnLaunched(LaunchActivatedEventArgs args)\r\n at cigaw.App.WinUI.App.OnLaunched(LaunchActivatedEventArgs args) in C:\\repos\\cigaw.App\\src\\cigaw.App\\cigaw.App\\Platforms\\Windows\\App.xaml.cs:line 29\r\n at Microsoft.UI.Xaml.Application.Microsoft.UI.Xaml.IApplicationOverrides.OnLaunched(LaunchActivatedEventArgs args)\r\n at ABI.Microsoft.UI.Xaml.IApplicationOverrides.Do_Abi_OnLaunched_0(IntPtr thisPtr, IntPtr args)" TargetSite: {Void <ThrowExceptionForHR>g__Throw|20_0(Int32)}

Relevant log output

No response

PureWeen commented 2 years ago

This should be fixed in P12

PedroCigaw commented 2 years ago

Tested in P12 now I get

System.ArgumentException HResult=0x80070057 Message=Source object type is not a projected type and does not inherit from a projected type. Arg_ParamName_Name Source=WinRT.Runtime StackTrace: at WinRT.CastExtensions.GetRefForObject(Object value) at WinRT.CastExtensions.As[TInterface](Object value) at WinRT.Interop.WindowNative.GetWindowHandle(Object target) at Microsoft.Maui.Essentials.Platform.get_CurrentWindowHandle() at Microsoft.Maui.Essentials.Platform.get_CurrentWindowId() at Microsoft.Maui.Essentials.Platform.get_CurrentAppWindow() at Microsoft.Maui.Essentials.DeviceDisplayImplementation.GetMainDisplayInfo() at Microsoft.Maui.Essentials.DeviceDisplay.add_MainDisplayInfoChanged(EventHandler1 value) at Microsoft.Maui.Controls.Application.SendStart() at Microsoft.Maui.Controls.Window.Microsoft.Maui.IWindow.Created() at Microsoft.Maui.LifecycleEvents.AppHostBuilderExtensions.<>c.b1_0(Window window) at Microsoft.Maui.Platform.ApplicationExtensions.<>c__DisplayClass1_0.b1(OnWindowCreated del) at Microsoft.Maui.LifecycleEvents.LifecycleEventServiceExtensions.InvokeLifecycleEvents[TDelegate](IServiceProvider services, Action`1 action) at Microsoft.Maui.Platform.ApplicationExtensions.CreateNativeWindow(Application nativeApplication, IApplication application, OpenWindowRequest args) at Microsoft.Maui.Platform.ApplicationExtensions.CreateNativeWindow(Application nativeApplication, IApplication application, LaunchActivatedEventArgs args) at Microsoft.Maui.MauiWinUIApplication.OnLaunched(LaunchActivatedEventArgs args) at Microsoft.UI.Xaml.Application.Microsoft.UI.Xaml.IApplicationOverrides.OnLaunched(LaunchActivatedEventArgs args) at ABI.Microsoft.UI.Xaml.IApplicationOverrides.Do_Abi_OnLaunched_0(IntPtr thisPtr, IntPtr args)

`