我想以像素为单位获取 Windows 商店应用程序页面中控件的位置。 能够为任意控件执行此操作会很好,但是目前只要弄清楚如何为按钮执行此操作就可以了!
我试过查看边距,但当像 Grid.Row="x" 或 HorizontalAlignment="Center"< 这样的机制时,它们似乎通常与实际位置无关 在 Xaml 中使用或以编程方式设置。无论定位控件的方法是什么,我都需要这种方法。
提前致谢!
编辑:
我正在通过设置 UI 进行测试:
<Grid x:Name="grid" Style="{StaticResource LayoutRootStyle}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="test" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Content="test"/>
</Grid>
然后修改'Grid.Column'、'Grid.Row'和Alignment特性。
请您参考如下方法:
您可以使用变换对象获取控件的位置。
<Button x:Name="btn" Content="Button" Margin="131,339,0,391" />
void BlankPage4_Loaded(object sender, RoutedEventArgs e)
{
var trans = test.TransformToVisual(null);
var point = trans.TransformPoint(new Windows.Foundation.Point());
}
