FrameworkElement.ActualHeight 属性

定义

获取 FrameworkElement 的呈现高度。 请参阅“备注”。

public:
 property double ActualHeight { double get(); };
double ActualHeight();
public double ActualHeight { get; }
var double = frameworkElement.actualHeight;
Public ReadOnly Property ActualHeight As Double

属性值

Double

double

对象的高度(以像素为单位)。 默认值为 0。 如果对象尚未加载且尚未参与呈现 UI 的布局传递,则可能会遇到默认值。

示例

此示例演示了一个常见方案,即在 UI 中使用一个或多个元素的 ActualHeight 设置其中一个相关元素或不同 元素的高度, 以便在操作后保持相同的净高度。 这通常是为了响应在打开或关闭元素或显示区域更改时调用的事件处理程序。

void SDKSample::WebViewControl::PageWithAppBar::BottomAppBar_Opened(Object^ sender, Object^ obj)
{
    // AppBar has Opened so we need to put the WebView back to its
    // original size/location.
    AppBar^ bottomAppBar = (AppBar^) sender;
    if (bottomAppBar != nullptr)
    {
        // Force layout so that we can guarantee that our AppBar's
        // actual height has height
        this->UpdateLayout();
        // Get the height of the AppBar
        double appBarHeight = bottomAppBar->ActualHeight;
        // Reduce the height of the WebView to allow for the AppBar
        WebView8->Height = WebView8->ActualHeight - appBarHeight;
        // Translate the WebView in the Y direction to reclaim the space occupied by the AppBar.  
        TranslateYOpen->To = -appBarHeight / 2.0;
        // Run our translate animation to match the AppBar
        OpenAppBar->Begin();
    }
}
void BottomAppBar_Opened(object sender, object e)
{
    // AppBar has Opened so we need to put the WebView back to its
    // original size/location.
    AppBar bottomAppBar = sender as AppBar;
    if (bottomAppBar != null)
    {
        // Force layout so that we can guarantee that our AppBar's
        // actual height has height
        this.UpdateLayout();
        // Get the height of the AppBar
        double appBarHeight = bottomAppBar.ActualHeight;
        // Reduce the height of the WebView to allow for the AppBar
        WebView8.Height = WebView8.ActualHeight - appBarHeight;
        // Translate the WebView in the Y direction to reclaim the space occupied by 
        // the AppBar.  Notice that we translate it by appBarHeight / 2.0.
        // This is because the WebView has VerticalAlignment and HorizontalAlignment
        // of 'Stretch' and when we reduce its size it reduces its overall size
        // from top and bottom by half the amount.
        TranslateYOpen.To = -appBarHeight / 2.0;
        // Run our translate animation to match the AppBar
        OpenAppBar.Begin();
    }
}
Private Sub BottomAppBar_Opened(sender As Object, e As Object)
    ' AppBar has Opened so we need to put the WebView back to its
    ' original size/location.
    Dim bottomAppBar As AppBar = TryCast(sender, AppBar)
    If bottomAppBar IsNot Nothing Then
        ' Force layout so that we can guarantee that our AppBar's
        ' actual height has height
        Me.UpdateLayout()
        ' Get the height of the AppBar
        Dim appBarHeight As Double = bottomAppBar.ActualHeight
        ' Reduce the height of the WebView to allow for the AppBar
        WebView8.Height = WebView8.ActualHeight - appBarHeight
        ' Translate the WebView in the Y direction to reclaim the space occupied by 
        ' the AppBar.  Notice that we translate it by appBarHeight / 2.0.
        ' This is because the WebView has VerticalAlignment and HorizontalAlignment
        ' of 'Stretch' and when we reduce its size it reduces its overall size
        ' from top and bottom by half the amount.
        TranslateYOpen.[To] = -appBarHeight / 2.0
        ' Run our translate animation to match the AppBar
        OpenAppBar.Begin()
    End If
End Sub

注解

注意

尽管它具有 ActualHeightProperty 后备字段,但 ActualHeight 不会引发属性更改通知,应将其视为常规属性,而不是依赖属性。

ActualHeight 是一个计算属性。 计算是布局传递的结果,其中对象根据其连续布局父级的逻辑在布局中调整大小。 有关详细信息,请参阅 使用 XAML 定义布局

由于布局系统的操作,ActualHeight 可能会对值进行多次或增量报告更改。 如果在布局仍在迭代时获取值,则布局系统可能仍在计算子对象所需的空间度量值、父对象的约束等。 由于该值基于实际的呈现传递,因此它可能会稍微落后于 Height 等属性的设置值,而 Height 可能是输入更改的基础。

出于 ElementName 绑定的目的,ActualHeight 在更改 (时不会发布更新,因为其异步和运行时计算性质) 。 请勿尝试使用 ActualHeight 作为 ElementName 绑定的绑定源。 如果方案需要基于 ActualHeight 进行更新,请使用 SizeChanged 处理程序。

适用于

另请参阅

  • ActualWidth
  • Height
  • <xref:Windows.UI.Xaml.FrameworkElement.SizeChanged%0a(frameworkelement_sizechanged.md)>