Xamarin屏幕旋转(OnSizeAllocated)

时间:2018-05-22 12:27:40

标签: c# xamarin xamarin.forms

我正在使用事件OnSizeAllocated在屏幕旋转时检查页面widthheight

protected override void OnSizeAllocated(double width, double height)
{
    base.OnSizeAllocated(width, height);                
    System.Diagnostics.Debug.WriteLine("width " + width);         
}

每个输出看起来像:

width 375
width 375
width 812
width 812

如何将输出限制为最后一个字符串?

width 812

1 个答案:

答案 0 :(得分:1)

我认为您只关心大小实际更改值时的更新,在这种情况下,在第二次和第三次执行之间。然后你可以做的是将大小存储在一个字段中,并在OnSizeAllocated执行时进行比较:

private double _width;
private double _height;

public MainPage
{
   _width = this.Width;
   _height = this.Height;
}

protected override void OnSizeAllocated( double width, double height )
{
   if ( _width != width || _height != height )
   {
       _width = width;
       _height = height;
       if ( width > height ) ScreenRotatedToLandscape( width, height );    
       //if ( height < width ) ScreenRotatedToPortrait( width, height);
   }
}

private void ScreenRotatedToLandscape( double newWidth, double newHeight )
{
    //your logic goes here
}