Xamarin Android、Xamarin Forms 方向更改 [英] Xamarin Android, Xamarin Forms orientation change
问题描述
使用 Xamarin Forms UI 框架的 Xamarin Android 应用.我检测到屏幕方向,并希望只允许特定页面的纵向和横向,而所有其他页面仅允许纵向.
Xamarin Android app that use the Xamarin Forms UI framework. I detect the screen orientation and want to only allow portrait and landscape for a specific Page but portrait only for all other pages.
我尝试调用 RequestOrientation 但这会强制方向保持不变并且不会再次触发我的方向更改通知.
I tried to call RequestOrientation but that force the orientation to stay the same and doesn't fire my orientation change notification again.
推荐答案
在你的 App.Xaml.cs 中有一个静态属性
Have a static property in your App.Xaml.cs
public static bool IsPortraitOnly { get; set; }
在所有页面中将其设置为 false
,并在您希望它仅为纵向的页面中设置为 true
.
Set it to false
in all pages and to true
in the page you want it to be Portrait only.
在你的 MainActivity
在 android 项目中,覆盖方法 OnConfigurationChanged
:
In your MainActivity
in android project, override the method OnConfigurationChanged
:
public override void OnConfigurationChanged(Configuration newConfig)
{
//using to prevent the OnCreate from firing when rotating or screen size is changing
if (App.IsPortraitOnly)
{
RequestedOrientation = ScreenOrientation.Portrait;
newConfig.Orientation = Orientation.Portrait;
}
base.OnConfigurationChanged(newConfig);
}
以下是android中可用的方向值:
The following are the available orientation values in android :
public enum Orientation
{
Landscape = 2,
Portrait = 1,
Square = 3,
Undefined = 0
}
这篇关于Xamarin Android、Xamarin Forms 方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!