我试图根据这样的点击/点击来设置图像的位置(注意“contentGrid”是Page的唯一子项并填充屏幕)
private void contentGrid_Tapped(object sender, TappedRoutedEventArgs e)
{
Uri uri = new Uri("ms-appx:///Images/Test.png");
BitmapImage bitmap = new BitmapImage(uri);
Image image = new Image();
image.Source = bitmap;
// The .png is a 30px by 30px image
image.Width = 30;
image.Height = 30;
image.Stretch = Stretch.None;
Point tappedPoint = e.GetPosition(contentGrid);
TranslateTransform posTransform = new TranslateTransform();
posTransform.X = tappedPoint.X;
posTransform.Y = tappedPoint.Y;
image.RenderTransform = posTransform;
contentGrid.Children.Add(image);
}
结果是Image of Test.png在单击时出现在页面上,但不在我点击/单击的位置。它在视觉上偏移了页面宽度的一半,并减少了页面高度的一半。
我试图理解这里的相对位置 - 将图像转换为用户实际点击/点击的最佳方式是什么?
答案 0 :(得分:4)
在您的案例中使用Grid
很棘手。
使用Canvas代替Grid
。
Canvas.SetLeft(image, tappedPoint.X);
Canvas.SetTop(image, tappedPoint.Y);
canvas.Children.Add(image);
答案 1 :(得分:3)
尝试设置图像的Margin
以显示鼠标点:
private void contentGrid_Tapped(object sender, TappedRoutedEventArgs e)
{
Uri uri = new Uri("ms-appx:///Images/Test.png");
BitmapImage bitmap = new BitmapImage(uri);
Image image = new Image();
image.Source = bitmap;
// The .png is a 30px by 30px image
image.Width = 30;
image.Height = 30;
image.Stretch = Stretch.None;
image.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
image.VerticalAlignment = System.Windows.VerticalAlignment.Top;
Point tappedPoint = e.GetPosition(contentGrid);
contentGrid.Children.Add(image);
image.Margin = new Thickness(tappedPoint.X, tappedPoint.Y, 0, 0);
}