2014-06-30 11 views
5

समायोजित करें जब मैं अपना पृष्ठ बना रहा हूं, तो मैं डिवाइस (एंड्रॉइड, आईओएस & विंडोज फोन) का अभिविन्यास जानना चाहता हूं। पृष्ठ में 3 कॉलमन्डिफिनिशन के साथ ग्रिड है और जैसे ही अभिविन्यास को परिदृश्य में बदल दिया गया है, उसके साथ 5 कॉलमंडिफ़िनिशन होना चाहिए।Xamarin.Forms - अभिविन्यास का पता लगाएं और पृष्ठ

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

तो यहां मैंने अपने कोड का परीक्षण करने के लिए 12 छवियां जोड़े। पेज पोर्ट्रेट-ओरिएंटेशन में अच्छा दिख रहा है और डिवाइस में लैंडस्केप-ओरिएंटेशन में कॉलम के बीच बहुत सी जगह है।

मैं भी जानकारी प्राप्त करने के लिए निर्भरता इंजेक्शन का उपयोग करने की कोशिश कर रहा हूं। निर्भरता सेवा अपना काम कर रही है, लेकिन मुझे डिवाइस के अभिविन्यास को पुनः प्राप्त करने में कोई सफलता नहीं है ...

+0

बनाने आप इस Git लिंक सहायक होगी https://github.com/xamarin/monotouch-samples/tree/master/Rotation –

+0

मैं इसे एक नज़र दूंगा। – Bart

उत्तर

1

xamarin.forms में, आप MessageCenter का उपयोग कर एंड्रॉइड भाग से अधिसूचना प्राप्त कर सकते हैं। 1.In साझा परियोजना

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 एंड्रॉयड परियोजना

[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

मैं एक ऐसी ही समस्या हल हो और बढ़िया पोस्ट जो शायद उपयोगी आप के लिए (नीचे हाइपरलिंक देखें) पर यह पाते हैं।

शॉर्टकट में: द्वारा

Page.Width < Page.Height 

उन्मुखीकरण जानकारी प्राप्त करें और ContentPage (या अन्य) के निर्माता इस जानकारी का उपयोग जब पेज

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

संबंधित मुद्दे