2014-06-30 4 views
5

Mi piacerebbe conoscere l'orientamento del dispositivo (Android, iOS & Windows Phone) nel momento in cui sto costruendo la mia pagina. La pagina ha una griglia con 3 columndefinitions e dovrebbe avere 5 columndefinitions non appena l'orientamento è cambiato in landscape.Xamarin.Forms - Rileva l'orientamento e regola la pagina

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

Quindi qui ho aggiunto 12 immagini per testare il mio codice. La pagina ha un bell'orientamento verticale e occupa molto spazio tra le colonne se il dispositivo è orientato in orizzontale.

Sto anche cercando di utilizzare l'iniezione di dipendenza per recuperare le informazioni. Il DependencyService sta facendo il suo lavoro, ma non ho alcun successo nel recuperare l'orientamento del dispositivo ...

+0

troverete questo link git utile https://github.com/xamarin/monotouch-samples/tree/master/Rotation –

+0

Gli darò un'occhiata. – Bart

risposta

1

In xamarin.forms, è possibile ottenere la notifica dalla parte di Android utilizzando MessageCenter. 1.In condivisa del progetto

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

[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

ho risolto un problema simile e lo trovo in grande post che forse utile per voi (vedi collegamento ipertestuale di seguito).

In collegamento: Trova Orientamento da

Page.Width < Page.Height 

e utilizzare queste informazioni nel costruttore di ContentPage (o altro) durante la creazione di pagina

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

Problemi correlati