2014-06-30 17 views
5

Chciałbym poznać orientację urządzenia (Android, iOS & Windows Phone) w czasie, gdy buduję swoją stronę. Strona ma siatkę z trzema niedopowiedziami i powinna mieć 5 niedoszacowań po zmianie orientacji na krajobraz.Xamarin.Forms - Wykryj orientację i dopasuj stronę

 Grid grid = new Grid 
     { 

      HorizontalOptions = LayoutOptions.Fill, 
      VerticalOptions = LayoutOptions.Fill, 
      RowSpacing = 15, 
      ColumnSpacing = 15, 
      Padding = new Thickness(15), 
      ColumnDefinitions = 
      { 
       new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }, 
       new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }, 
       new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) } 
      } 
     }; 

     for (int i = 0; i < 12; i++) 
     { 
      Image img = new Image() 
      { 
       Source = "ButtonBlue.png" 
      }; 
      //if(DependencyService.Get<IDeviceInfo>().IsPortraitOriented()) 
      //{ 
       grid.Children.Add(img, i % 3, i/3); 
      //} 
      //else 
      //{ 
      // grid.Children.Add(button, i % 5, i/5); 
      //} 
     } 

     this.Content = new ScrollView 
     { 
      Orientation = ScrollOrientation.Vertical, 
      Content = grid 
     }; 

Więc tutaj dodałem 12 zdjęć, aby przetestować mój kod. Strona wygląda dobrze w orientacji pionowej i ma dużo miejsca między kolumnami, jeśli urządzenie jest w orientacji poziomej.

Próbuję również użyć wtyczki zależności w celu pobrania informacji. Usługa DependencyService wykonuje swoją pracę, ale nie mam żadnego sukcesu w odzyskiwaniu orientacji urządzenia ...

+0

znajdziesz link git pomocny https://github.com/xamarin/monotouch-samples/tree/master/Rotation –

+0

Spojrzę na to. – Bart

Odpowiedz

1

W xamarin.forms możesz otrzymywać powiadomienia z części Android za pomocą centrum MessageCenter. 1.In Shared Projekt

public partial class MyPage : ContentPage 
{ 
    public MyPage() 
    { 
     InitializeComponent(); 

     Stack = new StackLayout 
     { 
      Orientation = StackOrientation.Vertical, 
     }; 
     Stack.Children.Add (new Button { Text = "one" }); 
     Stack.Children.Add (new Button { Text = "two" }); 
     Stack.Children.Add (new Button { Text = "three" }); 

     Content = Stack; 

     MessagingCenter.Subscribe<MyPage> (this, "Vertical", (sender) => 
     { 
      this.Stack.Orientation = StackOrientation.Vertical; 
      this.ForceLayout(); 
     }); 
     MessagingCenter.Subscribe<MyPage> (this, "Horizontal", (sender) => 
     { 
      this.Stack.Orientation = StackOrientation.Horizontal; 
      this.ForceLayout(); 
     }); 

    } 
    public StackLayout Stack; 
} 

2.In Android Projekt

[Activity (Label = "XamFormOrientation.Android.Android", MainLauncher = true, ConfigurationChanges = global::Android.Content.PM.ConfigChanges.Orientation | global::Android.Content.PM.ConfigChanges.ScreenSize)] 
public class MainActivity : AndroidActivity 
{ 
    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     Xamarin.Forms.Forms.Init (this, bundle); 

     SetPage (App.GetMainPage()); 
    } 
    public override void OnConfigurationChanged (global::Android.Content.Res.Configuration newConfig) 
    { 
     base.OnConfigurationChanged (newConfig); 

     if (newConfig.Orientation == global::Android.Content.Res.Orientation.Portrait) { 
      MessagingCenter.Send<MyPage> (null, "Vertical"); 
     } else if (newConfig.Orientation == global::Android.Content.Res.Orientation.Landscape) { 
      MessagingCenter.Send<MyPage> (null, "Horizontal"); 
     } 
    } 
} 
2

I rozwiązać podobny problem i go znaleźć na wielki post, który może być pomocny dla Ciebie (patrz odnośnik poniżej).

W skrócie: Sprawdzaj orientację przestrzenną za

Page.Width < Page.Height 

i wykorzystać te informacje w konstruktorze ContentPage (lub innych) podczas tworzenia strona

http://www.sellsbrothers.com/Posts/Details/13740