하나의 Xamarin Forms 프로젝트를 만들고 다른 장치에 따라 현재 화면 너비를 찾지 못했습니다.xamarin 폼에서 현재 화면 너비 가져 오기
나는 안드로이드와 ios에서 어떻게 할 수 있는지 알고 있지만 휴대용으로 너비를 찾는 방법이 필요하다.
하나의 Xamarin Forms 프로젝트를 만들고 다른 장치에 따라 현재 화면 너비를 찾지 못했습니다.xamarin 폼에서 현재 화면 너비 가져 오기
나는 안드로이드와 ios에서 어떻게 할 수 있는지 알고 있지만 휴대용으로 너비를 찾는 방법이 필요하다.
내 경우에는이 Application.Current.MainPage.Width
을 시도해 볼 수 있으며 휴대용 프로젝트 자체에서 장치 너비를 찾을 수 있습니다.
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/
) 너비가 나타나기 위해 Task.Delay (1000)를 기다려야 할 수도 있습니다 ... – pollaris
Task.Delay는 기다리는 좋은 방법이 아닙니다. 크기 속성을 얻으려면 대신 다음과 같은보기의 EventHandler 'SizeChanged'를 구독하고 싶습니다. SizeChanged + = (sender, e) => {...}; –
공통 코드
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는 앱이 출시 될 때, 당신은 공통 코드 어디에서나 사용할 수있을 것입니다 초기화됩니다.
이것은 나를 위해 작동하지 않습니다, 널 포인터 예외가 발생합니다 : ( – mathkid91