如何在 Xamarin.Forms
跨平台的特定页面上设置内容页面方向或屏幕方向。
我已经设置了 ScreenOrientation = ScreenOrientation
。
Portrait at property of all platform 但我想显示一些页面显示在两种变体中意味着纵向和横向,那么如何在 `xamarin.forms 的页面上设置。
在 xamarin forms 跨平台中设置屏幕方向不是设备方向而是页面方向一些页面以横向显示,一些页面以纵向显示
最佳答案
您可以通过为特定平台使用 DependencyService
创建依赖项来实现此目的。
尝试这样做:
界面
public interface IOrientationService
{
void Landscape();
void Portrait();
}
对于安卓:
[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
public class OrientationService: IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
}
对于 iOS:
[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
public class OrientationService: IOrientationService
{
public void Landscape()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
}
public void Portrait()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}
}
}
然后就可以这样使用了
DependencyService.Get<IOrientationService>().Landscape();
DependencyService.Get<IOrientationService>().Portrait();
希望对您有所帮助!
关于c# - 如何在 Xamarin.Forms 跨平台的特定页面上设置 ContentPage 方向或屏幕方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290561/