728x90
728x170

앱을 개발하다보면 기기 화면 크기에 따른 상대 비율로 컨트롤의 크기를 지정해야할 때가 있습니다.

당연히 화면 크기에 대한 처리가 기본적으로 제공될꺼라고 생각했는데 아니더군요;;

그래서 화면 크기를 알수 있는 방법을 소개합니다.

2가지 방법이 있습니다.

 

1. Xamarin.Essentials 사용하는 방법

NuGet 에서 Xamarin.Essentials 를 설치하고

using Xamarin.Essentials;

을 추가 후 아래와 같이 사용하면 됩니다.

// Get Metrics
var metrics = DeviceDisplay.ScreenMetrics;

// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = metrics.Orientation;

// Rotation (0, 90, 180, 270)
var rotation = metrics.Rotation;

// Width (in pixels)
var width = metrics.Width;

// Height (in pixels)
var height = metrics.Height;

// Screen density
var density = mainDisplayInfo.Density;

 

2. 기기별로 값을 가져오도록 직접 구현

.Net Standard 프로젝트의 App.xaml.cs 파일에 아래 처럼 속성을 추가합니다.

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace Test
{
    public partial class App : Application
    {
        public static int ScreenHeight { get; set; }
        public static int ScreenWidth { get; set; }

        public App ()
        {
            InitializeComponent();
            MainPage = new MainPage();
        }

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

 

Android 프로젝트의 MainActivity.cs 에 아래처럼 화면 크기를 가져오는 코딩을 추가합니다.

using Android.App;
using Android.Content.PM;
using Android.Gms.Ads;
using Android.OS;
using Microsoft.AppCenter.Push;

namespace Test.Droid
{
    [Activity(Label = "Test", Icon = "@drawable/test", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);

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

             LoadApplication(new App());
        }
    }
}

 

iOS 프로젝트의 AppDelegate.cs 에 아래처럼 화면 크기를 가져오는 코딩을 추가합니다.

using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;

namespace Test.iOS
{
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            App.screenWidth = (int)UIScreen.MainScreen.Bounds.Width;
            App.screenHeight = (int)UIScreen.MainScreen.Bounds.Height;

            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }
    }
}

 

이제 프로젝트 어디에서든 App.ScreenWidth, App.ScreenHeight 으로 화면 크기값을 알수 있습니다.

728x90
그리드형
Posted by kjun
,