简体   繁体   English

如何从 Xamarin Forms 在 a.xaml 文件中获取跨平台应用程序中的可用屏幕尺寸值?

[英]How to Get The Avaible Screen Size Value In Cross-Platform App From Xamarin Forms in a .xaml File?

In the app that I am developing I want to have a relative TranslationY value in order to have a responsive control center on my app that does not cover up the whole screen and can be used while typing on it.在我正在开发的应用程序中,我希望有一个相对的 TranslationY 值,以便在我的应用程序上拥有一个不会覆盖整个屏幕并且可以在输入时使用的响应式控制中心。 So I wrote the code below but TranslationY value is not relative and while typing the keyboard covers the control center.所以我写了下面的代码,但是 TranslationY 值不是相对的,并且在键入键盘时会覆盖控制中心。 Is there a way to get the number of available height of the screen and give a ratio to TranslationY.有没有办法获取屏幕可用高度的数量并给 TranslationY 一个比率。

<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"
             xmlns:map="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
             xmlns:pan ="http://xamarin.com/schemas/2014/forms"  
             xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
             mc:Ignorable="d"
             x:Class="GreenPath.MainPage">

    <Grid>
        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <map:Map MapType="Street" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
        </StackLayout>
        <yummy:PancakeView CornerRadius="25,25,0,0" Opacity="0.8" HorizontalOptions="FillAndExpand" BackgroundColor="White" TranslationY="550">
            <StackLayout VerticalOptions="FillAndExpand" Orientation="Vertical" Margin="5">
                <Image Source="down.png" VerticalOptions="Center" HorizontalOptions="Center" Scale="1"/>
                <Label  Text="X" TextColor="#3E454F" FontSize="40" FontFamily="segoeui.ttf" Margin="10,0"/>
                <SearchBar FontFamily="segoeui.ttf" Text="X" VerticalTextAlignment="Center" VerticalOptions="Fill" HorizontalTextAlignment="Start" IsReadOnly="True" HorizontalOptions="Fill" SearchButtonPressed="SearchBar_SearchButtonPressed" PlaceholderColor="#3E454F" TextColor="#3E454F" CancelButtonColor="#3E454F" Visual="Material" Keyboard="Default" Placeholder="Search a location" IsEnabled="True"/>
                <Label Text="X" FontFamily="segoeui.ttf" FontSize="25" VerticalOptions="Center" TextColor="#3E454F" Margin="10,0"/>
                <Grid Grid.Row="1" ColumnSpacing="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="add.png" VerticalOptions="Center" HorizontalOptions="Center" Scale="1" Grid.Column="0" />
                    <Image Source="add.png" VerticalOptions="Center" HorizontalOptions="Center" Scale="1" Grid.Column="1" />
                    <Image Source="add.png" VerticalOptions="Center" HorizontalOptions="Center" Scale="1" Grid.Column="2" />
                    <Image Source="add.png" VerticalOptions="Center" HorizontalOptions="Center" Scale="1" Grid.Column="3" />
                    <Image Source="add.png" VerticalOptions="Center" HorizontalOptions="Center" Scale="1" Grid.Column="4" />
                </Grid>
            </StackLayout>
        </yummy:PancakeView>

    </Grid>

</ContentPage>

From backend you can override the OnSizeAllocated Method.从后端您可以覆盖 OnSizeAllocated 方法。

    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
    }

You can get width and height from this method then you can assign the percent height to any of your component.您可以从此方法获取宽度和高度,然后可以将百分比高度分配给任何组件。

In app.xaml.cs在 app.xaml.cs

set:放:

public static double DeviceWidth, DeviceHeight;

For Android:对于 Android:

 #region For screen Height & Width  
 var pixels = Resources.DisplayMetrics.WidthPixels;
 var scale = Resources.DisplayMetrics.Density;
 var dps = (double)((pixels - 0.5f) / scale);
 var ScreenWidth = (int)dps;
 App.DeviceWidth = ScreenWidth;
 pixels = Resources.DisplayMetrics.HeightPixels;
 dps = (double)((pixels - 0.5f) / scale);
 var ScreenHeight = (int)dps;
 App.DeviceHeight = ScreenHeight;
 #endregion

for iOS:对于 iOS:

 #region For Screen Height & Width
  App.DeviceWidth = (int)UIScreen.MainScreen.Bounds.Width;
  App.DeviceHeight = (int)UIScreen.MainScreen.Bounds.Height;
  #endregion

So you can use in your xaml like this:因此,您可以像这样在 xaml 中使用:

double _deviceWidth = App.DeviceWidth;

You can get the device screen info by using Xamarin.Essentials: Device Display Information :您可以使用Xamarin 获取设备屏幕信息。要点:设备显示信息

Give a name to yummy:PancakeView :yummy:PancakeView

<yummy:PancakeView x:Name="pancakeView" CornerRadius="25,25,0,0" Opacity="0.8" HorizontalOptions="FillAndExpand" BackgroundColor="White" TranslationY="550">

In code behind, set the ratio:在后面的代码中,设置比率:

public MainPage()
{
    InitializeComponent();

    var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
    // Width (in pixels)
    var width = mainDisplayInfo.Width;
    // Height (in pixels)
    var height = mainDisplayInfo.Height;
    // Screen density
    var density = mainDisplayInfo.Density;

    var screenW = 0;
    var screenH =0;

    if (density != 0)
    {
         screenW = (int)(width / density);
         screenH = (int)(height / density);
    }

    pancakeView.TranslationY = 550 * 550 / screenH;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重命名Xamarin跨平台应用程序? - How to rename a Xamarin Cross-Platform App? iOS App(Xamarin)与iOS XAML App(Xamarin.Forms)与跨平台之间的区别是什么 - What are diffs between iOS App (Xamarin) vs. iOS XAML App (Xamarin.Forms) vs. Cross-Platform 如何在Xamarin Forms跨平台中绑定图像集合 - How to bind collection of images in Xamarin Forms Cross-Platform 用于跨平台Xamarin Forms的UserControl - UserControl for cross-platform Xamarin Forms 如何在 c#(xamarin 跨平台应用程序)中获取屏幕尺寸 - How to get screen dimensions in c# (xamarin cross platform app) 在 Xamarin.Forms 跨平台应用程序中使用 Amazon Cognito 用户池进行身份验证 - Authentication with Amazon Cognito User Pools in Xamarin.Forms Cross-Platform App Xamarin 跨平台应用解决方案构建失败? - Xamarin Cross-Platform App Solution Build failed? 无法绑定继承的属性(Xamarin跨平台应用程序) - Can't bind inherited property (Xamarin Cross-Platform app) 如何从解决方案中的文件加载XML文件,并使用嵌入式资源进行构建? 项目&gt;跨平台&gt; Xamarin.Forms - How to load an XML file from a file in the solution, build with Embedded Resource? Project > Cross Platform > Xamarin.Forms 如何从 Xamarin 项目中的跨平台页面打开和关闭 Android 活动 - How to open and close Android activity from cross-platform page in Xamarin project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM