我正在使用事件OnSizeAllocated
在屏幕旋转时检查页面width
或height
。
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
答案 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
}