2014-06-30 26 views
5

Tôi muốn biết hướng của thiết bị (Android, iOS & Windows Phone) tại thời điểm tôi đang xây dựng trang của mình. Trang này có một lưới với 3 độ sắc nét và cần có 5 columndefinitions ngay sau khi định hướng được thay đổi thành ngang.Xamarin.Forms - Phát hiện hướng và điều chỉnh trang

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

Vì vậy, ở đây tôi đã thêm 12 hình ảnh để kiểm tra mã của mình. Trang có vẻ đẹp theo hướng dọc và có nhiều khoảng trống giữa các cột nếu thiết bị ở hướng ngang.

Tôi cũng đang cố gắng sử dụng tính năng tiêm phụ thuộc để truy xuất thông tin. DependencyService đang thực hiện công việc của mình, nhưng tôi không có bất kỳ thành công nào để truy lục định hướng của thiết bị ...

+0

bạn sẽ tìm thấy liên kết git này hữu ích https://github.com/xamarin/monotouch-samples/tree/master/Rotation –

+0

Tôi sẽ xem xét. – Bart

Trả lời

1

Trong xamarin.forms, bạn có thể nhận được thông báo từ phần android bằng cách sử dụng MessageCenter. 1.In Shared Dự án

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 Project

[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

tôi giải quyết một vấn đề tương tự và tìm thấy nó trên bài tuyệt vời mà có thể hữu ích cho bạn (xem liên kết dưới đây).

Trong shortcut: Tìm hiểu định hướng bởi

Page.Width < Page.Height 

và sử dụng thông tin này trong constructor của ContentPage (hoặc khác) khi tạo trang

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

Các vấn đề liên quan