我想要做的是找到一种方法来了解对象(图像)的坐标(或任何属性),以便我可以使用它来编码在图像上放置椭圆的位置。我找到了一些讨论如何在stackpanel中找到texbox位置的内容,但是当我尝试使用该示例时,它会抛出一个异常“TargetInvocationException未处理”。也许我只是不明白如何引用不同的对象。我知道问题出现在第一个Dim声明中。
在Xaml中:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas x:Name="Can1">
<Image x:Name="Table1" Source="c:\images\table.jpg" Width="100" Height="100" Canvas.Left="75" Canvas.Top="75" />
<Ellipse Canvas.Left="100" Canvas.Top="100" Width="50" Height="50" Stroke="Green" StrokeThickness="4" />
</Canvas>
</Window>
在代码中:
Class MainWindow
Dim gt1 As GeneralTransform = Table1.TransformToAncestor(Can1)
Dim currentpoint As Point = gt1.Transform(New Point(0, 0))
End Class
答案 0 :(得分:1)
所以应该是:
Class MainWindow
Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Dim GT1 As GeneralTransform = Table1.TransformToAncestor(Can1)
Dim currentpoint As Point = GT1.Transform(New Point(0, 0))
End Sub
End Class