Reputation: 1075
I have an application which is perfectly fine in WPF, but when I implement that to in my Windows Phone 8 apps I am facing issues that I cannot using the same library in WP8.,
In WPF I create some random shapes and on mouse click on those shapes, I remove the shape, on which item the mouse is clicked.
Point pt = e.GetPosition((Canvas)sender);
if (null == pt)
{
pt = array;
}
HitTestResult result = VisualTreeHelper.HitTest(canvasArea, pt);
if (result != null)
{
canvasArea.Children.Remove(result.VisualHit as Shape);
}
This perfectly works fine. But the same code I cannot use in WP8, because the .Net libraries for WP8 and WPF are different.
However I managed to come till get the capture element by some google and by the help of some answers from SO
var element = (UIElement)sender;
var controls = VisualTreeHelper.FindElementsInHostCoordinates ( e.GetPosition ( element ), element );
the controls
gives me the value of the captured shape, but I don't know how to code to remove that items from the canvas. as controls
does not have anything like this.
canvasArea.Children.Remove(result.VisualHit as Shape);
Can anybody help me to remove. I tried with lambda expression too but didn't succeed.
Upvotes: 0
Views: 65
Reputation: 13003
VisualTreeHelper.FindElementsInHostCoordinates
returns a collection of UIElements (IEnumerable<UIElement>
), and as documentation,
For most operations, you will generally be interested only in the first UIElement in the set, which is the element that is visually the topmost rendered element for the visual composition...
You can get the first element and passing it to the Remove
method without casting (as
).
if (controls.Count() > 0)
{
canvasArea.Children.Remove(controls.First());
}
Upvotes: 1