[英]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.