Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net-3.5

wpf

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:

public static Point GetMousePosition(this Visual relativeTo)
{
    return relativeTo.PointFromScreen(GetMousePositionOnScreen());
}

Sometimes (usually when I've just switched between two tab controls) PointFromScreen throws an InvalidOperationException with the message This Visual is not connected to a PresentationSource.

On looking at the properties available on Visual I can't see any relating to a PresentationSource.

Given a Visual, how can I tell if it is going to throw that exception when I call PointFromScreen on it?

like image 251
Daniel Earwicker Avatar asked Jan 28 '10 11:01

Daniel Earwicker


People also ask

When use InvalidOperationException?

Remarks. InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments. Typically, it is thrown when the state of an object cannot support the method call.


4 Answers

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.

like image 56
ChrisF Avatar answered Oct 13 '22 13:10

ChrisF


I had a similar problem with a custom-made visual.

The solution was to defer the problematic task via Dispatcher (deferred execution with background priority in this case)...

public void MyProblematicDisplayMethod(Symbol TargetSymbol)
{
    this.HostingScrollViewer.BringIntoView(TargetSymbol.HeadingContentArea);
    ...
    // This post-call is needed due to WPF tricky rendering precedence (or whatever it is!).
    this.HostingScrollViewer.PostCall(
        (scrollviewer) =>
        {
            // in this case the "scrollviewer" lambda parameter is not needed
            var Location = TargetSymbol.Graphic.PointToScreen(new Point(TargetSymbol.HeadingContentArea.Left, TargetSymbol.HeadingContentArea.Top));
            ShowOnTop(this.EditBox, Location);
            this.EditBox.SelectAll();
        });
     ...
}

/// <summary>
/// Calls, for this Source object thread-dispatcher, the supplied operation with background priority (plus passing the source to the operation).
/// </summary>
public static void PostCall<TSource>(this TSource Source, Action<TSource> Operation) where TSource : DispatcherObject
{
    Source.Dispatcher.BeginInvoke(DispatcherPriority.Background,
        new DispatcherOperationCallback(delegate(Object state)
                                        { Operation(Source); return null; }),
        null);
}

I've used that PostCall in other ScrollViewer related rendering situations.

like image 27
Néstor Sánchez A. Avatar answered Oct 13 '22 15:10

Néstor Sánchez A.


I've found you can test IsVisible before calling PointFromScreen to protect against the InvalidOperationException.

like image 45
Drew Noakes Avatar answered Oct 13 '22 15:10

Drew Noakes


Late to the ballgame, but these responses helped me. I just wanted to point out That PresentaionSources are not connected to visual elements until they are completely loaded. So if in your constructor you are setting up events that may get fired prior to the visual element you are attempting to call PointFromScreen on is ready to be displayed on screen, then you will get that error. While as mentioned before you can wrap your method in something like:

public static Point GetMousePosition(this Visual relativeTo)
{
    if(PresentationSource.FromVisual(relativeTo) != null)
       return relativeTo.PointFromScreen(GetMousePositionOnScreen());
    else
       return new Point();
}

you could also consider not calling your method until you are sure that the visual has been render on screen at least once.

like image 26
DiamondDrake Avatar answered Oct 13 '22 14:10

DiamondDrake