I am using c#, Silverlight, WP7.
I have a Pivot in my app, and after each LoadedPivotItem event, I want to look at the UI tree to see where each item is located on the screen. Specifically, I am interested in the margin value, which is the x-offset from the left side of the screen.
Here is my code:
GeneralTransform gt = myUIElement.TransformToVisual(Application.Current.RootVisual);
double x = ((MatrixTransform)gt).Matrix.OffsetX;
Debug.WriteLine("Margin = {0}, UIElement = {1}", x, myUIElement);
When the application launches, this code is executed and an example of the printout is:
Margin = 12, UIElement = System.Windows.Controls.TextBlock
The problem is that once I swipe (a LoadedPivotItem event fires) and move to the pivot on the right, the margins become negative numbers. Example:
Margin = -468, UIElement = System.Windows.Controls.TextBlock
The weird thing is, even if I swipe back to the first Pivot, the values on the margins change to:
Margin = 492, UIElement = System.Windows.Controls.TextBlock
And if I keep swiping in one direction and end up back at the first Pivot, the values on the margins become:
Margin = -468, UIElement = System.Windows.Controls.TextBlock
I realize that these numbers are all offset by the screen size of 480, but it doesn’t seem consistent to me and I’d like to not deal with adjusting by the screen size.
Any suggestions as to why this is happening and how to fix it?
Thanks in advance.
I’ve figured out the easiest solution to this problem, and that is to %mod the x-offset with the screen-width to get the actual margins. Here I assume the screen-width is 480.
After pivoting left and right many times, I think I’ve figured out what is going on. The UIElements are going through positive and negative translations depending on which way you swipe, but swiping left and swiping right don’t cancel each other out since the (0,0) location of RootVisual changes with respect to the UIElement.