Son*_*ali 13 xamarin.ios portable-class-library xamarin.forms uwp
嗨,我需要知道Windows 10/8/8.1和iOS中的屏幕密度.我使用DisplayMetrics在Android中获得了屏幕密度,但我发现在UWP和iOS中没有这样的选项/属性.那么在UWP和IOS中是否有任何可以获得屏幕密度的属性.
Mat*_*gul 39
更新 - 2018年6月8日
Xamarin.Essentials为开发人员提供了适用于其移动应用程序的跨平台API.
Android,iOS和UWP提供独特的操作系统和平台API,开发人员可以通过C#利用Xamarin访问所有人.Xamarin.Essentials提供了一个单一的跨平台API,可以与任何Xamarin.Forms,Android,iOS或UWP应用程序一起使用,无论用户界面如何创建,都可以从共享代码访问这些应用程序. https://github.com/xamarin/Essentials
您现在可以搜索设备的显示信息,而无需管理所有单个平台代码段.
设备显示信息 - https://docs.microsoft.com/en-us/xamarin/essentials/device-display
在您的班级中添加对Xamarin.Essentials的引用:
using Xamarin.Essentials;
Run Code Online (Sandbox Code Playgroud)
以下信息通过API公开:
// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = mainDisplayInfo.Orientation;
// Rotation (0, 90, 180, 270)
var rotation = mainDisplayInfo.Rotation;
// Width (in pixels)
var width = mainDisplayInfo.Width;
// Height (in pixels)
var height = mainDisplayInfo.Height;
// Screen density
var density = mainDisplayInfo.Density;
Run Code Online (Sandbox Code Playgroud)
(Github)Xamarin.Essentials - https://github.com/xamarin/Essentials
(博客)Xamarin.Essentials - https://blog.xamarin.com/xamarin-essentials-cross-platform-apis-mobile-apps/
(Docs)Essentials入门 - https://docs.microsoft.com/en-us/xamarin/essentials/get-started
下面,我将特定操作系统的屏幕详细信息存储在static
位于Xamarin Forms App
类中的变量中
这些似乎都适用于我的测试.
(从我的github页面复制/粘贴 - https://github.com/mattregul/Xamarin_Screensize)
// Store off the device sizes, so we can access them within Xamarin Forms
// Screen Width = WidthPixels / Density
// Screen Height = HeightPixels / Density
App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScaleFactor = (double)Resources.DisplayMetrics.Density;
Run Code Online (Sandbox Code Playgroud)
// Store off the device sizes, so we can access them within Xamarin Forms
App.DisplayScreenWidth = (double)UIScreen.MainScreen.Bounds.Width;
App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height;
App.DisplayScaleFactor = (double)UIScreen.MainScreen.Scale;
Run Code Online (Sandbox Code Playgroud)
screensize
都是根项目命名空间// You decided which is best for you...
// Do you want Size of the App's View
// or
// Do you want the Display's resolution
// ######################################
// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;
// Size of screen's resolution
//screensize.App.DisplayScreenWidth = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
//screensize.App.DisplayScreenHeight = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Run Code Online (Sandbox Code Playgroud)
// Size of App's view
screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height;
screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width;
// Pixels per View Pixel
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling
// - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design
screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Run Code Online (Sandbox Code Playgroud)
// Size of App's view
screensize.App.DisplayScreenHeight = Window.Current.Bounds.Height;
screensize.App.DisplayScreenWidth = Window.Current.Bounds.Width;
screensize.App.DisplayScaleFactor = 1; // No scaling here? If you find a scaling for Windows 8.1, please let me know :)
Run Code Online (Sandbox Code Playgroud)
namespace screensize
{
public class App : Application
{
public static double DisplayScreenWidth = 0f;
public static double DisplayScreenHeight = 0f;
public static double DisplayScaleFactor = 0f;
public App()
{
string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" +
$"Width: {DisplayScreenWidth}\n" +
$"Height: {DisplayScreenHeight}\n" +
$"Scale Factor: {DisplayScaleFactor}";
// The root page of your application
var content = new ContentPage
{
Title = "Xamarin_GetDeviceScreensize",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
Text = ScreenDetails
}
}
}
};
MainPage = new NavigationPage(content);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9449 次 |
最近记录: |