locked
How to detect when a user control is completely onscreen? RRS feed

  • Question

  • Hi,

    How to detect when a user control is completely shown onto the phone screen? In other words, how to detect it is completely in the visible area of the screen?

    Thanks,

    Vaibhav

    Monday, February 11, 2013 12:09 PM

Answers

  • Hi,

    You can get the coordinates of a System.Windows.UIElement relative to any other UIElement. Here is an example to obtain the coordinates of a child element:

    GeneralTransform generalTransform = childElement.TransformToVisual( parentElement );
    Point childToParentCoordinates = generalTransform.Transform( new Point( 0, 0 ) );

    The parent element could be the LayoutRoot grid for example.

    More info:

    UIElement.TransformToVisual

    Hope this helps

    Mark


    Mark Chamberlain Sr. Escalation Engineer | Microsoft Developer Support | Windows Phone 8

    Friday, February 15, 2013 3:29 PM

All replies

  • Hi,

    It might depend, in part, on how the controls are set up, for example whether it is an element inside a LongListSelector.

    Can you explain the specific problem that you are working on? This info will help in providing alternate solutions.

    -Mark


    Mark Chamberlain Sr. Escalation Engineer | Microsoft Developer Support | Windows Phone 8

    Monday, February 11, 2013 9:01 PM
  • Hi,

    The User control could be made part of any ListItem or it could be added as it is. SO, the solution need to work in all the scenarios possible.

    The use case is, I want to show some content inside the control ONLY when that control comes onto the screen. Hope it clarifies.

    Thanks,

    Vaibhav

    Tuesday, February 12, 2013 4:35 AM
  • Hi Mark,

    Any clue on this? Is it possible by any chance?

    Thanks!

    Thursday, February 14, 2013 11:33 AM
  • Hi,

    I think the Silverlight architecture is designed such that you shouldn't need to worry about whether it is currently visible. Go ahead and layout your visual elements and Silverlight will then render only those elements that are actually visible on the screen.

    If you have a very large list and want to only render a subset of the list to avoid memory consumption and performance problems, the ListView and later, LongListSelector were designed to virtualize the list.

    http://blogs.msdn.com/b/ptorr/archive/2010/08/16/virtualizing-data-in-windows-phone-7-silverlight-applications.aspx

    Hope this helps,
    Mark


    Mark Chamberlain Sr. Escalation Engineer | Microsoft Developer Support | Windows Phone 8

    Thursday, February 14, 2013 4:27 PM
  • Hi Mark,

    Thanks for your answer, but it doesn't solve my use case.

    Let me clarify it further, I have a requirement where we need to count how many times a control is shown to the user. This can not be achieved until it is known that it is indeed on screen.

    Thanks,

    Vaibhav

    Friday, February 15, 2013 4:57 AM
  • Hi,

    You can get the coordinates of a System.Windows.UIElement relative to any other UIElement. Here is an example to obtain the coordinates of a child element:

    GeneralTransform generalTransform = childElement.TransformToVisual( parentElement );
    Point childToParentCoordinates = generalTransform.Transform( new Point( 0, 0 ) );

    The parent element could be the LayoutRoot grid for example.

    More info:

    UIElement.TransformToVisual

    Hope this helps

    Mark


    Mark Chamberlain Sr. Escalation Engineer | Microsoft Developer Support | Windows Phone 8

    Friday, February 15, 2013 3:29 PM
  • Hi Mark,

    In a list has 100 unread messages, when user scroll and see messages then I mark that messages are read. Each item in the loglist control is user control.

    Wednesday, April 3, 2013 4:51 PM