Xamarin 如何在共享代码中访问Android mobile的屏幕宽度和高度?

Xamarin 如何在共享代码中访问Android mobile的屏幕宽度和高度?,xamarin,xamarin.android,Xamarin,Xamarin.android,如何在共享代码中访问Xamarin.Android设备的屏幕宽度和高度?我无法使用资源。它无法在给定上下文中找到资源 为了清楚起见,我想展示不同方向(纵向和横向)的不同uı设计。因此,我希望在共享代码中达到平板电脑/手机的方向 这是我的第一个问题,问有没有办法检查它是风景画还是肖像画: 我认为共享代码中Android mobile的屏幕宽度和高度会对我有所帮助。onConfigurationChanged会帮助您检测设备是横向还是纵向。跟随 如果你真的想得到屏幕的宽度或高度。试试这个 /**

如何在共享代码中访问Xamarin.Android设备的屏幕宽度和高度?我无法使用资源。它无法在给定上下文中找到资源

为了清楚起见,我想展示不同方向(纵向和横向)的不同uı设计。因此,我希望在共享代码中达到平板电脑/手机的方向

这是我的第一个问题,问有没有办法检查它是风景画还是肖像画:


我认为共享代码中Android mobile的屏幕宽度和高度会对我有所帮助。

onConfigurationChanged
会帮助您检测设备是横向还是纵向。跟随

如果你真的想得到屏幕的宽度或高度。试试这个

/**
 * @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;
}

实际上,在xamarin.android中有一个很好的关于检测屏幕大小的小指南,可以在网站上找到

在onCreat方法的“Activity.cs”中,只需添加以下内容:

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
}

我认为这样做就可以了。

下面的代码将有助于根据手机屏幕的高度和宽度设计手机屏幕

基于此设计,它将适合所有移动分辨率

PCL:

 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
                  }
          }
     }
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
   }
 }
#region For Screen Height & Width

  App.screenWidth = (int)UIScreen.MainScreen.Bounds.Width;
      App.screenHeight = (int)UIScreen.MainScreen.Bounds.Height;
  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;
            }
      }
Xamarin.Android:

 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
                  }
          }
     }
MainActivity.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
   }
 }
#region For Screen Height & Width

  App.screenWidth = (int)UIScreen.MainScreen.Bounds.Width;
      App.screenHeight = (int)UIScreen.MainScreen.Bounds.Height;
  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;
            }
      }
#屏幕高度和宽度区域

    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

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
   }
 }
#region For Screen Height & Width

  App.screenWidth = (int)UIScreen.MainScreen.Bounds.Width;
      App.screenHeight = (int)UIScreen.MainScreen.Bounds.Height;
  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;
            }
      }
#端区

PCL:

 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
                  }
          }
     }
如果您使用MVVM模式,您必须在ViewModel中获得这些屏幕高度和屏幕宽度,然后为您的视图和布局提供高度和宽度

         // The below two lines will use to get the MOBILE SCREEN HEIGHT && WIDTH in ViewModel
          int heightScreen=App.screenHeight;
          int widthScreen=App.screenHeigh;
XAML设计:

 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
                  }
          }
     }
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>
C#设计:

 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
                  }
          }
     }

似乎您正在尝试设置一些控件相对于屏幕大小的高度和宽度。如果您使用的是Xamarin表格,则可以使用网格实现比例尺寸。使用Xamarin表单的整个想法是,您不必担心表单因素

因此,在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">
  <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>

另外,如果获取屏幕大小是不可避免的,我建议您使用dependency service来获取屏幕大小,而不是使用全局静态属性

我无法使用,因为我没有使用Xamarin.Forms。我使用的是Xamarin.Android。@KaanEKİN因为您在OP中包含了Xamarin.forms标记您得到了三个基于表单的答案,您需要更加小心,我已经提交了一个编辑。很抱歉,newhere@KaanEK没关系,伙计,当我有机会的时候,我会编辑我的答案,使之与android相关。非常感谢你们。我把这些代码放到MainActivity.cs中。现在我怎么称呼他们?