【问题标题】:Xamarin Forms get absolute value from GridLength Star or AutoXamarin Forms 从 GridLength Star 或 Auto 获取绝对值
【发布时间】:2018-09-02 10:47:04
【问题描述】:

我想用Grid布局类型在手机屏幕的下角定义一个小方块。

我正在尝试通过在代码隐藏中的 OnAppearing 方法中获取列宽,并使用它来设置行高。

在 Xaml 中我已经这样做了:

<Grid x:Name         ="iconGrid"
      BackgroundColor="Green">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="9*" />
        <ColumnDefinition Width="1.9*" />
        <ColumnDefinition Width="0.1*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="5*" />
        <RowDefinition Height="4.9*" />
        <RowDefinition Height="0.1*" />
    </Grid.RowDefinitions>
    <Frame BackgroundColor="Purple"
           Grid.Row       ="1"
           Grid.Column    ="1" />
</Grid>

然后在 OnAppearing 中我已经这样做了:

    protected override async void OnAppearing()
    {
        base.OnAppearing();

        iconGrid.RowDefinitions[1].Height = new GridLength(iconGrid.ColumnDefinitions[1].Width.Value);
        iconGrid.RowDefinitions[2].Height = new GridLength(iconGrid.ColumnDefinitions[2].Width.Value);
        var remainingHeight = iconGrid.Height - iconGrid.RowDefinitions[2].Height.Value - iconGrid.RowDefinitions[2].Height.Value;
        iconGrid.RowDefinitions[0].Height = new GridLength(remainingHeight);

        Debug.WriteLine("rows: " + iconGrid.RowDefinitions[0].Height + ", " + iconGrid.RowDefinitions[1].Height + ", " + iconGrid.RowDefinitions[2].Height);
    }

我的调试输出是:

rows: 811.8.Absolute, 1.9.Absolute, 0.1.Absolute

所以从调试输出看来,我只是获取列宽的 star 值,并将它们作为行的 absolute 值使用,其中当然不起作用。

我想要的是获取列的屏幕上绝对值,并将它们用作行的绝对值。

简而言之,我需要获取定义为星型的列的实际屏幕点大小。这可能吗?看来应该是的了。

【问题讨论】:

    标签: xamarin.forms code-behind grid-layout


    【解决方案1】:

    认为我迷失了你的所有需求,也迷失了自己。对不起,关于那个。

    从显示器中获取绝对值很好.. 很复杂,但有一个新的 Nuget 包仍在预发布中,称为 Xamarin.Essentials,它将使您能够获取手机的宽度和高度并且根据您的设计,应该很容易确定您需要的尺寸。

    要安装 Nuget 包 Xamarin.Essential,首先勾选复选框:包括预发行版。

    添加到代码隐藏:

    using Xamarin.Essentials;
    

    您可以获取方向、旋转、密度、宽度和高度,最后一个不会随着旋转而改变 - 如果 Genymotions Android 模拟器可以相信的话。

    var metrics = DeviceDisplay.ScreenMetrics;
    // Orientation (Landscape, Portrait, Square, Unknown)
    var orientation = metrics.Orientation;
    // Rotation (0, 90, 180, 270)
    var rotation = metrics.Rotation;
    // Width (in pixels)
    var width = metrics.Width;
    // Height (in pixels)
    var height = metrics.Height;
    // Screen density
    var density = metrics.Density;
    

    据我所知,它为您提供了可用的页面高度,即减去顶部任何菜单的高度。

    更多信息在这里:https://docs.microsoft.com/en-us/xamarin/essentials/?context=xamarin/xamarin-forms

    --- 原帖“相对布局”---

    <RowDefinition 
    RelativeLayout.HeightConstraint="{ConstraintExpression 
    Type=RelativeToParent,
    Property=Height,
    Factor=0.1,
    Constant=0}" />
    
    <ColumnDefinition 
    RelativeLayout.WidthConstraint="{ConstraintExpression 
    Type=RelativeToParent, 
    Property=Heigth, 
    Factor=0.1,
    Constant=0}"
    

    【讨论】:

    • 这并不能解决制作正方形的问题,除非我误解了。您的意思是在高度示例中设置相对于父级宽度的高度吗?
    • 你想要很多,我想我迷失在所有这些需求中,但也许你真正需要的是别的东西。有一个 Pre-release Nuget Xamarin.Essentials 可让您获得可用空间的宽度和高度:docs.microsoft.com/en-us/xamarin/essentials/… 它应该很容易根据您的需要计算行高的可用空间,因此也适用于正方形。
    • 看起来你原来的帖子的例子可以用一个单词改变。与其让高度参考父高度和宽度参考父宽度,不如让它们都参考宽度。那不行吗?
    • 一开始我也是这么想的,但我觉得不太好。估计你可以试试。您还可以将 RelativeLayouts 应用于各种元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多