In WPF, under what circumstances does Visual.PointFromScreen throw InvalidOperationException?

前端 未结 5 666
深忆病人
深忆病人 2021-01-07 23:07

Suppose I wanted to do this, so I can find the current position of the mouse relative to a Visual, without needing access to a specific mouse event:

<         


        
5条回答
  •  悲哀的现实
    2021-01-08 00:00

    There's a static method PresentationSource.FromVisual which:

    Returns the source in which a provided Visual is presented.

    I know this doesn't solve the underlying problem, but you could check that the Visual is connected to a PresentationSource before calling PointFromScreen. It would prevent the exception, but you'd need to do some more investigation as to why it wasn't connected in the first place.

提交回复
热议问题