333 333 - 16 days ago 5
C# Question

DisplayInformation in UWP Background Task

I want to access the

DisplayInformation
class from a background task in my UWP that takes an image and resizes it from its native resolution to the size of the user's screen, so that it can be set as a wallpaper.

However, when I attempt to use
DisplayInformation.GetForCurrentView()
, an exception is thrown, saying that the method must be called from a thread associated with a
CoreWindow
. How can I avoid this?

Any solution will work as long as it will give me the complete width and height of the user's screen in raw pixels.

Answer

This API requires a CoreWindow because you could have multiple monitors on your system, and it needs to know which one to pick (it picks the one that is currently showing the window). The best solution is to query the properties when your foreground app is running, and then save them for later use. (Of course this won't be perfect if the user has two or more monitors of different sizes and you pick the "wrong" one, or they upgrade the monitor, etc. but it's the best you can do).

Comments