Search code examples
c#windows-phone-8.1screendimensionswin-universal-app

Window.Current.Bounds throws HRESULT E_FAIL has been returned from a call to a COM component


I'm building a Windows universal SDK that will retrieve user and device dimensions and I'm trying to get the screen size of the device from a non XAML (or related) class. When trying to get the bounds I receive "HRESULT E_FAIL has been returned from a call to a COM component".

var bounds = Window.Current.Bounds;

My 'Dimensions' class that contains the above code is instantiated (via a couple of parent classes) from the App method of the App.xaml.cs file is this too soon to get the current window?


Solution

  • Too soon. Rather than caching the data early I'd query it when needed (actually, you'll need to cache for off-dispatcher thread use). For a Universal app you'll need to handle the window size changing and multiple windows.

    If you are mobile specific you should be able to get the Window bounds in OnLaunched, but for an SDK it'll be more transparent to handle the CoreApplication.Activated event. You can wire this up in the App constructor and then get the window details when it fires and the ActivationKind tells you it's a windowed activation.