2017-02-17 92 views
2

Как настроить ориентацию страницы или ориентацию экрана на определенной странице в кросс-платформе Xamarin.Forms.Как установить ориентацию содержимого или ориентацию экрана на перкулярной странице на платформе Xamarin.Forms

I комплект ScreenOrientation = ScreenOrientation.

Портрет в собственности всей платформы, но я хочу показать показ некоторых страниц в обоих вариантах: «Портрет» и «Пейзаж», поэтому как установить на странице в «xamarin.forms».

набор ориентации экрана не устройство мудро, но установить на стр-мудры некоторые страницы показывают в ландшафтном & некоторых страниц шоу в портретном в Xamarin формах кросс- платформы

+0

установка ориентации экрана не устройство мудрый, но заданный по-странице некоторые страницы показывают в Landscape & some page show in Portrait in xamarin forms cross-platform – sonu

ответ

0

Так вот как я это делаю в Android. Вы должны добавить код MainActivity.cs

[Activity(...., ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

или ваше приложение будет сгенерировано исключение, если пользователь поворачивает свое устройство.

Затем добавьте это:

//To check if device is allowed to rotate 
private bool _allowLandscape; 

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity 
{ 
    private bool _allowLandscape; 

    protected override void OnCreate(Bundle bundle) 
    { 
     switch (Device.Idiom) 
     { 
      case TargetIdiom.Phone: 
       RequestedOrientation = ScreenOrientation.Portrait; 
       break; 
      case TargetIdiom.Tablet: 
       RequestedOrientation = ScreenOrientation.Landscape; 
       break; 
     } 

     base.OnCreate(bundle); 

     //Setup additional stuff that you need 

     //Calls from the view that should rotate 
     MessagingCenter.Subscribe<GraphicsView>(this, "graphic", sender => 
     { 
      _allowLandscape = true; 
      OnConfigurationChanged(new Configuration()); 
     }); 

     //When the page is closed this is called 
     MessagingCenter.Subscribe<GraphicsView>(this, "return", sender => 
     { 
      _allowLandscape = false; 
      OnConfigurationChanged(new Configuration()); 
     }); 

     LoadApplication(new App()); 
    } 

    public override void OnConfigurationChanged(Configuration newConfig) 
    { 
     base.OnConfigurationChanged(newConfig); 

     switch (newConfig.Orientation) 
     { 
      case Orientation.Landscape: 
       switch (Device.Idiom) 
       { 
        case TargetIdiom.Phone: 
         if (!_allowLandscape) 
         { 
          LockRotation(Orientation.Portrait); 
         } 
         break; 
        case TargetIdiom.Tablet: 
         LockRotation(Orientation.Landscape); 
         break; 
       } 
       break; 
      case Orientation.Portrait: 
       switch (Device.Idiom) 
       { 
        case TargetIdiom.Phone: 
         if (!_allowLandscape) 
         { 
          LockRotation(Orientation.Portrait); 
         } 
         break; 
        case TargetIdiom.Tablet: 
         LockRotation(Orientation.Landscape); 
         break; 
       } 
       break; 
      case Orientation.Undefined: 
       if (Device.Idiom == TargetIdiom.Phone && _allowLandscape) 
       { 
        LockRotation(Orientation.Landscape); 
       } 
       else if (Device.Idiom == TargetIdiom.Phone && !_allowLandscape) 
       { 
        LockRotation(Orientation.Portrait); 
       } 
       break; 
     } 
    } 

    private void LockRotation(Orientation orientation) 
    { 
     switch (orientation) 
     { 
      case Orientation.Portrait: 
       RequestedOrientation = ScreenOrientation.Portrait; 
       break; 
      case Orientation.Landscape: 
       RequestedOrientation = ScreenOrientation.Landscape; 
       break; 
     } 
    } 
} 

Надеется, что это будет работать для вас.

2

Вы можете сделать это, создав зависимость, используя DependencyService для определенных платформ.

Попробуйте сделать это:

интерфейса

public interface IOrientationService 
    { 
     void Landscape(); 
     void Portrait(); 
    } 

для Android:

[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; 
     } 
    } 
} 

Для прошивки:

[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(); 

Надеюсь, это поможет!