首先显示 MainPage ,然后点击按钮并调用 SecondPage 。 Everthing看起来很好,但是当我点击后退按钮时(显示器的方向应该是纵向的,但是我会得到一些奇怪的行为,比如纵向和横向(水平)视图。
Page1 (右侧垂直方向) - > Page2 (右侧水平方向) - >(后退按钮操作) - > Page1 (错误的垂直方向)。
when firstly showing when click back button
的MainPage:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
var fullScreenVideoPage = new SecondPage();
NavigationPage.SetHasNavigationBar(fullScreenVideoPage, false);
await Navigation.PushAsync(fullScreenVideoPage);
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send(this, "preventLandScape");
}
}
XAML (MainPage):
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage"
BackgroundColor="Black">
<StackLayout x:Name="stackLayout" Orientation="Horizontal">
<Image Source="icon.png"
WidthRequest="150"
HeightRequest="150"
HorizontalOptions ="CenterAndExpand"
VerticalOptions="CenterAndExpand">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnTapGestureRecognizerTapped" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
</ContentPage>
SecondPage:
public partial class SecondPage : ContentPage
{
public SecondPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send(this, "showLandscapeOrientation");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Send(this, "showPortraitOrientation");
}
async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
await Navigation.PushAsync(new NavigationPage(new MainPage()));
}
}
MainActivity:
[Activity(Label = "App1", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
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);
LoadApplication(new App());
//allowing the device to change the screen orientation based on the rotation
MessagingCenter.Subscribe<SecondPage>(this, "showLandscapeOrientation", sender =>
{
RequestedOrientation = ScreenOrientation.Landscape;
});
//during page close setting back to portrait
MessagingCenter.Subscribe<SecondPage>(this, "preventLandScape", sender =>
{
RequestedOrientation = ScreenOrientation.Portrait;
});
MessagingCenter.Subscribe<MainPage>(this, "showPortraitOrientation", sender =>
{
RequestedOrientation = ScreenOrientation.Portrait;
});
}
}
答案 0 :(得分:1)
问题是您为showPortraitOrientation
订阅了MainPage
,但是您从preventLandScape
致电MainPage
。
因此,您可以修改OnAppearing()
MainPage
中的代码,如下所示:
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send(this, "showPortraitOrientation");
}
其他问题是,在从横向屏幕切换回纵向屏幕时,您可能需要重新绘制布局。对于Android平台,您可以使用以下代码:
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send(this, "showPortraitOrientation");
//force redraw
this.InvalidateMeasure();
}
protected override void InvalidateMeasure()
{
Task.Delay(200).Wait();
base.InvalidateMeasure();
}