在 Windows Phone8.1 中获取 ActualWidth
Getting ActualWidth in Windows Phone8.1
我想在我的 WP8.1 应用程序中获取图像的实际宽度。在呈现页面之前无法确定宽度(即为零),其他解决方案建议在页面加载事件中处理此问题,如下面的基本示例所示。但即使在这里,img.ActualWidth
也为零。
如何在页面呈现后立即检索 img.ActualWidth
一次?
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e) {
Debug.WriteLine(img.ActualWidth);
}
}
和
<Page
x:Class="Page_Loaded.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Page_Loaded"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Image x:Name="img" Source="/image.jpg" />
</Grid>
</Page>
使用 SizeChanged 事件处理程序。 当 ActualHeight 或 ActualWidth 属性 更改 FrameworkElement 上的值时发生。
ActualWidth是计算出来的属性。计算是布局传递的结果,其中对象根据其连续布局父级的逻辑在布局中调整大小。
ActualWidth 的默认值为 0。如果对象尚未加载且尚未参与布局过程,则可能会遇到默认值呈现 UI. (这发生在你的情况下)
由于布局系统的操作,ActualWidth 可以有多个或增量报告的值更改。如果您在布局仍在迭代时获取值,则布局系统可能仍在计算子对象所需的 space 度量、父对象的约束等。由于该值是基于实际的渲染过程,它可能会略微落后于 Width 等属性的设置值,这可能是输入变化的基础。
出于 ElementName 绑定的目的,ActualWidth 在更改时不会 post 更新(由于其异步和 运行 时间计算的性质)。不要尝试将 ActualWidth 用作 ElementName 绑定的绑定源。 如果您的方案需要基于 ActualWidth 进行更新,请使用 SizeChanged 处理程序。
SizeChanged 每当对象的大小(ActualHeight 或 ActualWidth)发生变化时触发,这是在 Measure 和 Arrange passes 完成之后。处理 SizeChanged 事件的原因之一是查看元素的 ActualHeight 与 ActualWidth 的比率是否因新布局而发生变化。
我想在我的 WP8.1 应用程序中获取图像的实际宽度。在呈现页面之前无法确定宽度(即为零),其他解决方案建议在页面加载事件中处理此问题,如下面的基本示例所示。但即使在这里,img.ActualWidth
也为零。
如何在页面呈现后立即检索 img.ActualWidth
一次?
public sealed partial class MainPage : Page {
public MainPage() {
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e) {
Debug.WriteLine(img.ActualWidth);
}
}
和
<Page
x:Class="Page_Loaded.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Page_Loaded"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Image x:Name="img" Source="/image.jpg" />
</Grid>
</Page>
使用 SizeChanged 事件处理程序。 当 ActualHeight 或 ActualWidth 属性 更改 FrameworkElement 上的值时发生。
ActualWidth是计算出来的属性。计算是布局传递的结果,其中对象根据其连续布局父级的逻辑在布局中调整大小。
ActualWidth 的默认值为 0。如果对象尚未加载且尚未参与布局过程,则可能会遇到默认值呈现 UI. (这发生在你的情况下)
由于布局系统的操作,ActualWidth 可以有多个或增量报告的值更改。如果您在布局仍在迭代时获取值,则布局系统可能仍在计算子对象所需的 space 度量、父对象的约束等。由于该值是基于实际的渲染过程,它可能会略微落后于 Width 等属性的设置值,这可能是输入变化的基础。
出于 ElementName 绑定的目的,ActualWidth 在更改时不会 post 更新(由于其异步和 运行 时间计算的性质)。不要尝试将 ActualWidth 用作 ElementName 绑定的绑定源。 如果您的方案需要基于 ActualWidth 进行更新,请使用 SizeChanged 处理程序。
SizeChanged 每当对象的大小(ActualHeight 或 ActualWidth)发生变化时触发,这是在 Measure 和 Arrange passes 完成之后。处理 SizeChanged 事件的原因之一是查看元素的 ActualHeight 与 ActualWidth 的比率是否因新布局而发生变化。