如何在共享代码中访问Xamarin.Android设备的屏幕宽度和高度?我无法使用资源。它在给定的上下文中找不到资源。
要清楚,我想为不同的方向(肖像和风景)展示不同的设计。因此,我希望在共享代码中达到平板电脑/手机的方向。
这是我的第一个问题,询问是否有办法检查它是横向还是纵向: A way to check if it is in the landscape or portrait mode in Xamarin.Android
我认为共享代码中Android手机的屏幕宽度和高度对我有帮助。
答案 0 :(得分:0)
onConfigurationChanged
可帮助您检测设备是横向还是纵向。关注this
如果你真的想获得屏幕宽度或高度。试试这个
/**
* @param activity Context
* @return a integer value represent the width of physical device
*/
public static int getScreenHeight(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.heightPixels;
}
/**
* @param activity Context
* @return a integer value represent the width of physical device
*/
public static int getScreenWidth(Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
答案 1 :(得分:0)
实际上,在xamarin developer guide website上可以找到xamarin.android中检测屏幕大小的优秀指南。
在' Activity.cs'在onCreat方法中只需添加以下内容:
public static Rotation screenOrientation;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var metrics = Resources.DisplayMetrics;
var widthInDp = ConvertPixelsToDp(metrics.WidthPixels);
var heightInDp = ConvertPixelsToDp(metrics.HeightPixels);
if(widthInDp > heightInDp)
{
screenOrientation = Rotation.Landscape;
}
else
{
screenOrientation = Rotation.Portrait;
}
}
如果你想轻易区分风景和肖像,我会在某个地方使用一点枚举。
public enum Rotation
{
Landscape,
Portrait
}
我认为这样的事情可以解决问题。
答案 2 :(得分:0)
以下代码将有助于根据移动屏幕的高度和宽度设计移动屏幕。
基于此的设计将适合所有移动分辨率。
<强> PCL:强>
<强> App.cs 强>
public class App : Application
{
public static int screenHeight, screenWidth;
public App()
{
MainPage = new UserLogin();
//The root page of your application
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
<强> Xamarin.Android:强>
<强> MainActivity.cs 强>
#region用于屏幕高度&amp;宽度
var pixels = Resources.DisplayMetrics.WidthPixels;
var scale = Resources.DisplayMetrics.Density;
var dps = (double)((pixels - 0.5f) / scale);
var ScreenWidth = (int)dps;
App.screenWidth = ScreenWidth;
//RequestedOrientation = ScreenOrientation.Portrait;
pixels = Resources.DisplayMetrics.HeightPixels;
dps = (double)((pixels - 0.5f) / scale);
var ScreenHeight = (int)dps;
App.screenHeight = ScreenHeight;
<强> Xamarin.iOS 强>
<强> AppDelegate.cs 强>
#region For Screen Height & Width
App.screenWidth = (int)UIScreen.MainScreen.Bounds.Width;
App.screenHeight = (int)UIScreen.MainScreen.Bounds.Height;
#endregion
<强> PCL:强>
如果您使用MVVM模式,则必须在ViewModel中获取这些ScreeenHeight和ScreenWidth然后为视图和布局提供高度和宽度。
// The below two lines will use to get the MOBILE SCREEN HEIGHT && WIDTH in ViewModel
int heightScreen=App.screenHeight;
int widthScreen=App.screenHeigh;
XAML设计:
<强> UserLogin.xaml 强>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-
SreenSizeDemo;assembly=SreenSizeDemo" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SreenSizeDemo.UserLogin">
<StackLayout>
<Label x:Name="lblTitle" HorizontalOptions="FillAndExpand"/>
<Image x:Name="imgProfile" Source="Logo.png" />
<StackLayout>
<ContentPage>
<强> UserLogin.xaml.cs 强>
public partial class UserLogin : BaseContentPage
{
// Here you get the MOBILE SCREEN HEIGHT && WIDTH
int heightScreen=App.screenHeight;
int widthScreen=App.screenHeight;
public UserLogin()
{
lblTitle.FontSize=height=Screen/36.8;
//The above value is equal to fontsize =20
imgProfile.HeightRequest=heightScreeen/10;
imgProfile. WidthRequest=heightScreeen/10;
}
}
C#Design:
public class UserLogin: ContentPage
{
// Here you get the MOBILE SCREEN HEIGHT && WIDTH
int heightScreen=App.screenHeight;
int widthScreen=App.screenHeight;
public UserLogin()
{
Label lab = new Label()
{
FontSize = heightScreen/ 36.8
//the above value is equal to fontsize =20
};
Image imgProfile=new Image()
{
Source="Logo.png",
HeightRequest=heightScreeen/10,
WidthRequest=heightScreeen/10
}
}
}
答案 3 :(得分:0)
您似乎正在尝试相对于屏幕尺寸设置某些控件的高度和宽度。如果您使用的是Xamarin Forms,则可以使用Grid实现比例尺寸。关于使用Xamarin Forms的整个想法是你不必担心外形。
因此,在您的Xaml中,您可以通过以下方式使用Grid
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-SreenSizeDemo;assembly=SreenSizeDemo" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SreenSizeDemo.UserLogin">
<Grid x:Name="ContentGrid">
<Grid.RowDefinitions>
<!--This row will resize depending upon the size of the content within it-->
<RowDefinition Height="Auto">
<!--This will take 10% of your total screen height-->
<RowDefinition Height="0.1*">
<!--This will take rest of the available the screen height (i.e. ScreenHeight - Row0Height - Row1Height)-->
<RowDefinition Height="*">
</Grid.RowDefinitions>
<Label x:Name="lblTitle" Grid.Row="0" HorizontalOptions="FillAndExpand"/>
<!--You don't need to set both height and width of the image, setting either of them is enough. Unless your ImageAscpect property neither of these AspectFill, AspectFit values-->
<Image x:Name="ImgProfile" Grid.Row="1" Source="Logo.png"/>
<!--Rest of your contents go in third row-->
</Grid>
<ContentPage>
在后面的代码中你可能要做的唯一事情是为标签控件设置FontSize
。在这种情况下,你可以设置相对于屏幕尺寸的字体大小。网格的高度。
public UserLogin()
{
lblTitle.FontSize = ContentGrid.Height/10;
}
此外,如果获取屏幕大小是不可避免的,我建议您使用依赖服务来获取屏幕大小,而不是使用全局静态属性