c# - 如何从 C# 代码获取屏幕大小到 XAML?

标签 c# android xaml xamarin

我知道如何从代码中获取屏幕尺寸。但是我怎样才能将这些数字传递给 XAML 以便我可以正确地绘制屏幕呢?我正在尝试制作一个包含 3 行的基本网格。中间的高度应与设备屏幕宽度相同。我现在拥有的是:

<Grid BackgroundColor="Black">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="I DONT KNOW WHAT TO PUT HERE :S" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="1" BackgroundColor="Blue" Margin="10" />
</Grid>

对我来说,这似乎是一件非常基本的事情,但我找不到任何关于如何做到这一点的例子。

最佳答案

您可以通过Xamarin.Essentials: Device Display Information获取设备信息,创建一个名为 Constant 的结构,并在其中定义 ScreenWidthScreenHeight

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        // Get Metrics
        var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;

        // Orientation (Landscape, Portrait, Square, Unknown)
        var orientation = mainDisplayInfo.Orientation;

        // Rotation (0, 90, 180, 270)
        var rotation = mainDisplayInfo.Rotation;

        // Width (in pixels)
        var width = mainDisplayInfo.Width;

        // Height (in pixels)
        var height = mainDisplayInfo.Height;

        // Screen density
        var density = mainDisplayInfo.Density;

    }
}

public struct Constant
{
    public static double ScreenWidth = DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density;
    public static double ScreenHeight = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;
}

在xaml中使用它:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:local="clr-namespace:App105"
             x:Class="App105.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="{Binding Source={x:Static local:Constant.ScreenWidth}}" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label Text="Top Left" BackgroundColor="Red" Grid.Row="0" Grid.Column="0" />
        <Label Text="Top Right" BackgroundColor="Blue" Grid.Row="0" Grid.Column="1" />
        <Label Text="middle Left" BackgroundColor="Green" Grid.Row="1" Grid.Column="0" />
        <Label Text="middle Right" BackgroundColor="Yellow" Grid.Row="1" Grid.Column="1" />
        <Label Text="Bottom Left" BackgroundColor="Orange" Grid.Row="2" Grid.Column="0" />
        <Label Text="Bottom Righ" BackgroundColor="Pink" Grid.Row="2" Grid.Column="1" />
    </Grid>

</ContentPage>

关于c# - 如何从 C# 代码获取屏幕大小到 XAML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59507378/

相关文章:

c# - 将亚音速类序列化为 JSON

android - Kotlin 注释格式在 Android Studio 中自动与上一行换行

c# - 在 DataTemplate 中显示数据绑定(bind)的 StackPanel

c# - 从 C# 静默执行 MSI 包

c# - 当路径超过 260 个字符时出现 System.IO.DirectoryNotFoundException

c# - MonoGame/XNA 在 Spritebatch 中绘制多边形

android - Dagger-Hilt:@ViewModelInject 没有注入(inject) MyViewModel 并崩溃?

java - 如何使用 sse android 发布通知

c# - 转换器不能应用于需要 IValueConverter 类型的属性

c# - Xaml C# 中 Windows 8 商店应用程序的游戏循环