xamarin - 使用 Xamarin Form 获取设备属性?

标签 xamarin xamarin.forms

我正在使用 xamarin 表单设计一个跨平台应用程序。 每个页面/ View /表单都是从代码隐藏设计的。现在我想读取用户使用的设备的高度和宽度。根据这些值,我想放置一些页眉和页脚。

最佳答案

为了获取 Xamarin.Forms 解决方案中的屏幕宽度(或高度),我通常添加以下几行代码:

  1. 在共享代码中定义公共(public)静态属性,最好在 App.cs 中:

    static public int ScreenWidth;
    
  2. AppDelegate.cs 中的 FinishedLaunching 开头针对 iOS 进行初始化:

    App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
    
  3. MainActivity.csOnCreate 中针对 Android 进行初始化(如所述 here )

    App.ScreenWidth = (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
    

    (通过除以密度,得到与设备无关的像素。)

我没有使用 Windows Phone,但应该有一个等效的命令。当然,获取屏幕高度的工作原理类似。

现在您可以在代码中的任何位置访问 App.ScreenWidth

关于xamarin - 使用 Xamarin Form 获取设备属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543063/

相关文章:

android - 无效的资源目录名称 obj\Debug\res menu.png

xaml - VS 2017 NetStandard XAML 智能感知

c# - 在 Xamarin 中将内容页面转换为导航页面

Xamarin 在 XAML 中更改字体

c# - 使用 Xamarin.Forms 创建 ASP.Core WebAPI

c# - Xamarin:实例化 Xamarin.Droid 或 Xamarin.iOS 类

xamarin.forms - Xamarin Release Build 出现错误,无法解析 System.Void Xamarin.Forms.Log::Warning(System.String,System.String)

ios - Xamarin iOS 中心 TableViewCell subview

xamarin - 在 Xamarin.Forms RelativeLayout 中使用常量和因子?

xamarin - Azure 应用服务离线同步中的客户端事务