2
votes

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.

1
"The user's screen" is ambiguous. There could be more than one screen (PC multimon, phone continuum). There could an infinite number of screens (HoloLens). That's why you need a core window. So the system knows which screen you are asking about.Raymond Chen

1 Answers

5
votes

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).