2016-08-11 3 views

답변

11

내 경우에는이 Application.Current.MainPage.Width을 시도해 볼 수 있으며 휴대용 프로젝트 자체에서 장치 너비를 찾을 수 있습니다.

+5

이것은 나를 위해 작동하지 않습니다, 널 포인터 예외가 발생합니다 : ( – mathkid91

3

ContentPage에있을 때 Width 속성을 사용할 수 있습니다. 예 :

protected override void OnAppearing() 
{ 
    base.OnAppearing(); 
    double w = this.Width; 
} 

레이아웃 단계에서 설정됩니다. 따라서 페이지가 초기화 된 후에 만 ​​사용할 수 있습니다. 그렇지 않으면 -1입니다. 합니다 (한 OnCreate 방법에 MainActivity.cs)

public static int ScreenHeight {get; set;} 
public static int ScreenWidth {get; set;} 

안드로이드 부 (App.xaml.cs를 단위)는

https://developer.xamarin.com/api/property/Xamarin.Forms.VisualElement.Width/

+0

) 너비가 나타나기 위해 Task.Delay (1000)를 기다려야 할 수도 있습니다 ... – pollaris

+0

Task.Delay는 기다리는 좋은 방법이 아닙니다. 크기 속성을 얻으려면 대신 다음과 같은보기의 EventHandler 'SizeChanged'를 구독하고 싶습니다. SizeChanged + = (sender, e) => {...}; –

5

공통 코드

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

iOS 파트 (FinishedLaunching 메서드의 AppDelegate.cs)

App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height; 
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width; 

그래서 App.ScreenHeight 및 App.ScreenWidth는 앱이 출시 될 때, 당신은 공통 코드 어디에서나 사용할 수있을 것입니다 초기화됩니다.

+0

이것은 iOS에서 – Oz0234

+0

인 0을 반환합니다. , 안드로이드에서 작동하는 것 같다. – Oz0234

관련 문제